# Physical Character Controller CharacterController

In some typical scenarios, such as controlling the protagonist to move in a world full of obstacles, you need to have complete control over the displacement of an object (the protagonist), but you also need to allow the protagonist to be able to be obstructed (collision body) Blocked. The physical role controller is a component specially designed for this type of scene.

The physical character controller **does not need to be used with a rigid body, and it comes with a capsule collider. The shape of the capsule collider can be set in the component properties.

# Important methods and events

Method name Description
move Move the physical character controller, it will be blocked by obstacles.
simpleMove Similar to move, it moves at a given speed and is affected by gravity.
reposition Teleport physical character controller (similar to modifying Rigidbody.position).
Event Name Event Object Description
onControllerColliderHit ControllerColliderHit This event will be generated if it is blocked by obstacles in the process of move().

To add event callbacks to the physical role controller, please refer to [Methods for adding event callbacks to collision bodies](./collider.md#important attributes and events).

# The movement of the physical character controller

Like a rigid body, you can directly modify the position of Transform to achieve the effect of teleporting the physical character controller. During the teleportation, it will not be blocked by any objects, and will definitely appear at the target location. If there are ordinary rigid bodies at the target location, they will be squeezed away in the next frame.

In addition to teleporting, you should choose the move method for movement in general. After using move, the physical character controller will immediately move to the target position in a straight line. If there are other collision bodies (static collision bodies or rigid bodies) at the landing position, they will be blocked along a straight line, other rigid bodies will not be pushed, and they will not intersect with other collision bodies.

In the process of move, the physical character controller must keep the top of the head up, and can only rotate along the y-axis. If you encounter slopes or steps, the physical character controller can simulate the behavior of climbing up the slopes and steps, and the maximum height that can be climbed can be set in the component properties.

In practice, you can call move or simpleMove every frame to move a fixed short distance when the game character moves, simulating the walking behavior of the character.

# The difference with Kinematics Rigid Body

The actual performance of the physical character controller is similar to the kinematic rigid body, it will not be displaced or rotated during the physics simulation. But the difference with the kinematic rigid body is:

  1. The physical character controller cannot be Trigger;
  2. The physical character controller does not have a movePosition method, so it cannot squeeze other rigid bodies along a straight line.
点击咨询小助手