CharacterController
The Character Controller sample of the C# Component Samples demo demonstrates the following: C# Component Samples演示的CharacterController示例演示以下内容:
- Implementation of the first-person character controller using the First Person Controller component assigned to PlayerDummy使用分配给PlayerDummy的First Person Controller组件实现第一人称角色控制器
- Setting the collision capsule设置防撞舱
- Detecting intersection via shape-surface collision and detailing collision via the surface intersection通过形状表面 collision 检测相交并通过表面相交详细描述碰撞
First Person Controller Component第一人称控制器组件#
The FirstPersonController component implements basic first-person player controls. The Player itself consists of a camera and a capsule shape to detect collisions. The component has the following settings available for adjustment: FirstPersonController组件实现基本的第一人称播放器控件。播放器本身由相机和胶囊形状组成,用于检测碰撞。该组件具有以下可调整的设置:
- Input 输入值
- Gamepad Input 游戏手柄输入
- Body parameters required for movements 身体运动所需的参数
- Camera parameters 相机参数
- Movement parameters 运动参数
- Auto Stepping for walking through obstacles with a certain height and slope 自动步进,用于通过一定高度和坡度的障碍物
- Objects Interaction for simple interaction with physical objects 对象交互用于与物理对象的简单交互
- Advanced Settings 高级设置
- Debug settings to facilitate debugging Debug 设置以方便调试
Creating a Player创建一个球员#
To create a player, you need to create a new Player Dummy (Create -> Camera -> Dummy) and add it to the scene.要创建播放器,需要创建一个新的Player Dummy(Create -> Camera -> Dummy)并将其添加到场景中。
Enable the Main Player option for this Player in the Parameters window. Then add the FirstPersonController component to this node.在Parameters窗口中为此播放器启用Main Player选项。然后将FirstPersonController组件添加到该节点。
If you save the world and run the application, you can control a player with the standard settings.如果您保存世界并运行,则可以使用标准设置控制播放器。
The player is represented by a capsule with the pivot located at the bottom of the capsule. When the player is created, the transformation is corrected based on the Y axis direction of the player. The Z axis is always directed upwards.玩家由一个胶囊代表,其枢轴位于胶囊的底部。创建播放器后,将根据播放器的Y轴方向校正转换。 Z轴始终指向上方。
If the camera is assigned from the editor, its position and direction are based on the player's position and rotation in the world: the horizontal angle is the player's rotation angle along the Z axis, and the vertical angle is the actual camera's angle.如果从编辑器分配了摄像机,则摄像机的位置和方向取决于玩家在世界上的位置和旋转:水平角是玩家沿Z轴的旋转角,垂直角是实际摄像机的角角度。
Settings设定值#
The FirstPersonController component contains the following groups of settings that are displayed in the Node tab of the Parameters window after assigning the component to a node. FirstPersonController组件包含在将组件分配给节点之后在Parameters窗口的Node选项卡中显示的以下设置。
Input Settings输入设定#
The Input group allows setting keys to control the character using the keyboard and mouse: moving forward and backward, left and right, run, jump, and crouch modes. To assign the keys, Input.KEY enums are used. You can also adjust the mouse sensitivity that affects the camera rotation speed. Input组允许设置键来使用键盘和鼠标控制字符:向前和向后,向左和向右,运行,跳转和蹲伏模式。要分配键,使用Input.KEY枚举。您还可以调整影响相机旋转速度的鼠标灵敏度。
Input from the keyboard/mouse and gamepad are processed simultaneously. The action with a higher deviation has a priority.同时处理来自键盘/鼠标和游戏手柄的输入。偏差较大的动作优先。
Gamepad Input Settings游戏手柄输入设置#
The Gamepad Input group allows setting the gamepad control. Current settings are simplified: one stick can be set for horizontal movement, and the other for the camera rotation. Deadzone deviation is set for both sticks, and camera sensitivity can be set additionally. Any other actions can be set using Input.GAMEPAD_BUTTON keys. Gamepad Input组允许设置游戏手柄控件。当前设置得到简化:可以将一根摇杆设置为水平移动,而另一根摇杆设置为摄像机旋转。两个摇杆均设置了死区偏差,并且可以额外设置相机灵敏度。可以使用Input.GAMEPAD_BUTTON键设置其他任何操作。
Input from the keyboard/mouse and gamepad are processed simultaneously. The action with a higher deviation has a priority.同时处理来自键盘/鼠标和游戏手柄的输入。偏差较大的动作优先。
Body Settings身体设定#
The Body group contains settings to adjust the player's physical body and shape defined by a capsule. Body组包含用于调整玩家的身体和胶囊定义的形状的设置。
Use Object Body | If enabled, the body and shape of the object this component is assigned to are used.
To enable this option, the object should have the Dummy Body and at least one Capsule shape. If either body or shape is missing, they will be created automatically based on the values set in the parameters below with the corresponding warning in the console.To enable this option, the object should have the Dummy Body and at least one Capsule shape. If either body or shape is missing, they will be created automatically based on the values set in the parameters below with the corresponding warning in the console. To enable this option, the object should have the Dummy Body and at least one Capsule shape. If either body or shape is missing, they will be created automatically based on the values set in the parameters below with the corresponding warning in the console.要启用此选项,对象应具有Dummy Body和至少一个Capsule形状。如果缺少主体或形状,则会根据以下参数中设置的值自动创建它们,并在控制台中显示相应的警告。 |
---|---|
Capsule Radius | The radius of the capsule that defines the player's shape.定义玩家形状的胶囊的半径。 |
Capsule Height | The height of the capsule that defines the player's shape. Keep in mind that this value defines only the height of the cylindrical part. The full height of the capsule is calculated as capsule height + 2 * capsule radius. 封套的高度,定义了玩家的形状。请记住,此值仅定义圆柱零件的高度。胶囊的整个高度计算为capsule height + 2 * capsule radius。
|
Physics Intersection Mask | The mask that is used for selective detection of physics intersections (with other physical objects having bodies and collider shapes, or ray intersections with the collider geometry). 遮罩 用于选择性检测物理相交(与其他具有物体和碰撞体形状的物理对象,或与碰撞体几何形状相交的射线)。 |
Collison Mask | The mask that is used to detect collisions of the shape with other physical objects. 遮罩,用于检测形状与其他物理对象的碰撞。 |
Exclusion Mask | The mask that is used to prevent collisions of the shape with other physical objects. 遮罩 ,用于防止形状与其他物理对象发生碰撞。 |
Camera Settings相机设定#
The Camera group contains settings to adjust the camera. Camera组包含用于调整相机的设置。
Camera Mode | The mode of assigning the camera to the player:
|
---|---|
FOV | The camera's field of view, in degrees.摄像机的视场,以度为单位。 |
Near Clipping | Distance to the near clipping plane of the player's viewing frustum, in units.到玩家观看的视锥的近剪切平面的距离,以单位为单位。 |
Far Clipping | Distance to the far clipping plane of the player's viewing frustum, in units.到玩家观看的视锥的远剪切平面的距离,以单位为单位。 |
Camera Position Offset | Distance from the camera to the player's position.从摄像机到播放器位置的距离。 |
Min Vertical Angle | The minimum vertical angle of the camera, i.e. maximum possible angle to look up.摄像机的最小垂直角度,即可以仰视的最大角度。 |
Max Vertical Angle | The maximum vertical angle of the camera, i.e. maximum possible angle to look down.摄像机的最大垂直角度,即向下看的最大可能角度。 |
Movement Settings动作设定#
The Movement group contains all settings required for the character movements. Movement组包含角色移动所需的所有设置。
Use Jump | The flag that enables jumping and all relevant parameters in the player logic.允许在播放器逻辑中启用跳跃和所有相关参数的标志。 | ||||||||
---|---|---|---|---|---|---|---|---|---|
Use Crouch | The flag that enables crouching and all relevant parameters in the player logic.
If the player is crouching, and the Crouch button is released, it will raise only if there are no obstacles above. If the player raises, and there is an obstacle above or the Crouch button is pressed again, the player takes the crouch state again.If the player is crouching, and the Crouch button is released, it will raise only if there are no obstacles above. If the player raises, and there is an obstacle above or the Crouch button is pressed again, the player takes the crouch state again. If the player is crouching, and the Crouch button is released, it will raise only if there are no obstacles above. If the player raises, and there is an obstacle above or the Crouch button is pressed again, the player takes the crouch state again.如果播放器蹲下,并且释放了Crouch按钮,则只有在上面没有障碍物时它才会升起。如果玩家举手,并且上方有障碍物,或者再次按下Crouch按钮,则玩家将再次进入蹲伏状态。 |
||||||||
Use Run | The flag that enables running and all relevant parameters in the player logic.在播放器逻辑中启用运行和所有相关参数的标志。 | ||||||||
Use Run Default | The flag that sets running as a default state of the player's movement. If this state is enabled, the walk state is enabled by using the Run Key.该标志将跑步设置为玩家移动的默认状态。如果启用此状态,则使用 Run Key 启用步行状态。 | ||||||||
Crouch Speed | Horizontal speed used for crouching.用于蹲伏的水平速度。 | ||||||||
Walk Speed | Horizontal speed used for walking.用于步行的水平速度。 | ||||||||
Run Speed | Horizontal speed used for running.用于运行的水平速度。 | ||||||||
Ground Acceleration | Horizontal acceleration applied when the player is on the ground.玩家在地面上时施加的水平加速度。 | ||||||||
Air Acceleration | Horizontal acceleration applied when the player is in the air.玩家在空中时施加的水平加速度。 | ||||||||
Ground Damping | Damping of the horizontal speed when the player is on the ground.玩家在地面上时水平速度的衰减。 | ||||||||
Air Damping | Damping of the horizontal speed when the player is in the air.玩家在空中时水平速度的衰减。 | ||||||||
Jump Power | Jump power of the player in the Run, Walk and Idle modes.在“跑步”,“步行”和“空闲”模式下玩家的跳跃力。 | ||||||||
Crouch Jump Power | Jump power of the player in the Crouch mode.在Crouch模式下播放器的跳跃力。 | ||||||||
Crouch Height | Height of the camera when the player is crouching. The minimum value equals to capsule radius x 2, the maximum height is equal to the capsule height.播放器蹲伏时相机的高度。 最小值等于 capsule radius x 2,最大高度等于 胶囊高度 。 | ||||||||
Crouch Transition Time | Time required for transition between the crouching and standing states.蹲伏状态和站立状态之间转换所需的时间。 | ||||||||
Max Ground Angle | Angle up to which the ground is considered to be even. If the angle is exceeded, the character can't stand still and slides down the slope. If the character climbs up the hill and its angle is more than this value, the character wouldn't climb up.认为地面平整的角度。如果超过该角度,角色将无法站立并滑下斜坡。如果角色爬上山坡并且其角度大于此值,则角色不会爬上。 | ||||||||
Max Ceiling Angle | The value that defines the maximum angle at which the surface is considered to be the ceiling. If the surface the top part of the capsule contacts with has an angle that is less than this value, the character is considered to have hit the ceiling: its vertical speed becomes equal to 0 and the character starts to fall down. If the surface above the character has an angle that exceeds this value, the character slides along this surface.该值定义将表面视为天花板的最大角度。如果胶囊顶部接触的表面的角度小于此值,则认为角色已撞击天花板:其垂直速度等于0,角色开始下落。如果字符上方的表面的角度超过此值,则字符将沿该表面滑动。 | ||||||||
Check Move Ray Offset | Offset of the ray that checks the surface it would potentially move to. The minimum value is 0, the maximum is 0.1 unit.
|
||||||||
Check Move Mask | Mask used to sort out intersections with the surface.用于整理与曲面的相交的蒙版。 | ||||||||
Wall Stop Sliding Angle | Horizontal angle between the direction ray and contact point of the capsule with the wall. If the angle is less than this value, the player doesn't slide along the wall.
方向射线与胶囊与壁的接触点之间的水平角度。如果角度小于此值,则播放器不会沿着墙壁滑动。 |
Auto Stepping Settings自动步进设置#
The Auto Stepping group of settings fine-tunes automatic climbing upon obstacles up to a certain height. The Auto Stepping process consists of three phases: raising the player to the obstacle height, moving it onto the obstacle and solving collisions, and dropping down on the surface of this obstacle and solving collisions. Auto Stepping组设置可微调自动爬升到一定高度的障碍物。自动步进过程包括三个阶段:将玩家提升到障碍物的高度,将其移动到障碍物上并解决碰撞,并放下该障碍物的表面并解决碰撞。
Use Auto Stepping | The flag to toggle the Auto Stepping option.用于切换自动步进选项的标志。 |
---|---|
Min Step Height | Minimum height of an obstacle for Auto Stepping to be applied. This is required to avoid applying Auto Stepping on even surfaces.要应用自动步进的障碍物的最小高度。这是避免在平坦表面上应用自动步进所必需的。 |
Max Step Height | Maximum height of an obstacle that can be climbed upon with Auto Stepping.通过自动步进可以攀爬的障碍物的最大高度。 |
Max Stair Angle | Maximum slope angle for Auto Stepping to be applied. If the character steps on a surface with a slope, the angle of which is greater than the Max Stair Angle value, this last step is canceled.要应用自动步进的最大倾斜角度。如果角色在坡度大于其最大楼梯角度值的倾斜表面上踩踏,则最后一步将被取消。 |
Check Stair Ray Offset | Offset of the ray that checks the level of the potential stair for Auto Stepping. This offset is vertical and in the direction of the potential movement.检查自动步进的潜在阶梯级别的射线的偏移量。此偏移量是垂直的,并且沿潜在的移动方向。 |
Stairs Detection Angle | Horizontal angle that defines the area for checking the contacts of the capsule with other objects in the movement direction. Allows avoiding false Auto Stepping near walls.水平角度,该角度定义用于检查胶囊在移动方向上与其他对象的接触的区域。允许避免在墙壁附近错误的自动步进。 |
Stair Detection Mask | The mask for checking intersections with the stairs for Auto Stepping. If you have stairs with a complex geometry and want to simplify it for the Auto Stepping process, disable this mask for the complex geometry and enable for the simplified model.用于检查与台阶的相交的蒙版,以进行自动步进。如果您的楼梯具有复杂的几何图形,并希望在“自动步进”过程中对其进行简化,请为复杂的几何图形禁用此蒙版,并为简化的模型启用。 |
Objects Interaction Settings对象交互设置#
The Objects Interaction group contains settings that regulate simple physical interaction of the player with other physical objects. Objects Interaction组包含用于调节播放器与其他物理对象的简单物理交互的设置。
Use Objects Interaction | The flag to toggle interaction with other physical objects. These are the objects that have the Rigid Body assigned.用于切换与其他物理对象的交互的标志。这些是分配了刚体的对象。 |
---|---|
Impulse Multiplier | Multiplier for the impulse applied to the Rigid Body the player collides with.施加到与玩家碰撞的刚体上的脉冲的乘数。 |
Advanced Settings高级设置#
The Advanced Settings group contains auxiliary settings for the player update. Advanced Settings组包含用于播放器更新的辅助设置。
Player Fps | Minimum update framerate for the player. If the current FPS is less than this value, the player is updated several times per frame.播放器的最小更新帧速率。如果当前FPS小于此值,则播放器每帧更新几次。 |
---|---|
Collision Iterations | Number of iterations for resolving collisions.解决冲突的迭代次数。 |
Contacts Buffer Size | Maximum number of contacts processed for a collision.为冲突处理的最大联系人数。 |
Heavy Contacts Count | Maximum number of contacts up to which the Collision Iterations value is applied at processing contacts. If the number of contacts exceeds this value, then only one iteration is used to avoid performance drop.在处理联系人上应用“碰撞迭代”值的最大联系人数。如果联系人数量超过此值,则仅使用一次迭代来避免性能下降。 |
Debug Settings调试设置#
The Debug Settings group contains settings for debugging information. To see the debug info, the project should be run with the Debug configuration set. Debug Settings组包含调试信息的设置。要查看调试信息,应在设置 Debug配置的情况下运行项目。
Debug Camera | Enabling the Debug Camera creates a third-person camera that observes the player. This camera is controlled using the cursor arrows and + and - for changing the distance to the player. Enabling Use Fixed Angles positions this camera behind the player and makes it view the player's direction. 启用Debug Camera会创建一个观察玩家的第三人称摄像机。使用光标箭头和+和-来控制此摄像机,以更改到播放器的距离。 启用Use Fixed Angles可以将此摄像机放置在播放器后面,并使其查看播放器的方向。 |
---|---|
Debug Visualizer | Enabling the Debug Visualizer displays the selected debug information:
|
Debug Profiler | Enabling the Debug Profiler displays the selected debug information in the Profiler:
|
Debug Colors | Color pickers for setting the colors to be used for displaying the information in the Visualizer and Profiler.颜色选择器,用于设置用于在Visualizer和Profiler中显示信息的颜色。 |
访问演示项目源代码#
您可以学习和修改此演示的源代码来创建自己的项目。 要访问源代码,请执行以下操作:
- 在Demos部分中找到C# Component Samples演示并点击Install(安装)(如果您尚未安装)。
- 安装成功后,demo会出现在Installed部分,您可以点击Copy as Project基于此创建项目 演示。
- 在打开的 Create New Project 窗口中,在相应字段中输入新项目的名称,然后单击Create New Project.
- 现在您可以点击Open Code IDE查看和修改默认IDE中的源代码,或者点击Open Editor打开项目 在 UnigineEditor中。