A character controller is a special object in the physics engine that is used to move a character throughout a scene and make it collide with other geometry. A character controller is typically an upright capsule that abstractly represents the space that a character occupies.
The character controller provides the following functionality:
- Move throughout a scene, collide with and slide along walls
- Fall to the ground, slide down steep slopes
- Climb up shallow slopes
- Step over small obstacles
- Climb stairs
- Stand and crouch with different capsule sizes
- Push dynamic objects
- Get pushed by kinematic objects
- Ride on kinematic platforms
On top of these basic features, the character controller implements many details of movement. For example, while jumping or falling, a game may allow the player some degree of control. Such details are very game specific, though, and there is no one-size-fits-all solution.
Consequently, the character controller functionality is split up into multiple classes, and you are encouraged to implement your own logic:
ezCharacterControllerComponent: An abstract base class for all character controllers, with a minimal interface. Other (script) code typically uses this interface to instruct the character to move, according to the player’s input or the decision of an AI controlled character. This class is not actually specific to the PhysX plugin.
ezPxCharacterShapeComponent: A base class for exposing the raw functionality of the PhysX character controllers. Specifically
ezPxCharacterCapsuleShapeComponentprovides an implementation for the most commonly used capsule controller. If you implement a custom character controller, you can still save some work by reusing this, instead of calling the PhysX code yourself. On the other hand, you may want to write a custom implementation for this, if you need to adjust the very low level PhysX behavior for colliding and interacting with other bodies.
ezPxCharacterControllerComponent: An implementation of
ezCharacterControllerComponentthat is provided as an example and as a decent starting point. It implements behavior similar to old-school first-person shooter games, such as Half-Life 2. Depending on how significantly different behavior you want, you can either derive from this class and override some parts, or copy the entire code and rewrite everything as desired. The latter approach may be the better solution, as
ezPxCharacterControllerComponentmay get changes over time that you don’t desire for your game.
The player object is often the most complicated object in a game. The character controller only provides the locomotion aspect, but this is often coupled tightly to the overall game logic. For example, the player may move slower or be disallowed to jump while carrying an object. Many of these aspects can be handled by an overall player logic script. Other aspects, like the details of the characters velocity while sliding down a slope or jumping through the air, have to be implemented directly inside a character controller component.
The Testing Chambers sample contains a prefab called Player.ezPrefab, which demonstrates how to build your own player object. The top level node contains both a PhysX Character Capsule Shape component (for the raw movement functionality), as well as a PhysX Character Controller component. You could replace the latter with a custom character controller component, to test out entirely different movement behavior.
ezPxCharacterShapeComponent Component Properties
CollisionLayer: The collision layer to use for colliding with other geometry.
Mass: The mass of the character affects how much force it applies to objects it is standing on.
MaxStepHeight: The maximum height of obstacles that the CC will step over automatically. Unless
ConstrainedClimbModeis enabled, the CC may step over higher objects, though, because of the rounded bottom of the capsule controller.
MaxSlopeAngle: The maximum angle of slopes that the character controller may walk up.
ForceSlopeSliding: Determines whether the CC can stand on slopes steeper than
MaxSlopeAngle. If this is enabled, it will be forced to slide down the slope.
ConstrainedClimbMode: Tries to prevent a capsule shaped CC from stepping over obstacles taller than
MaxStepHeight. Costs extra performance.
ezPxCharacterCapsuleShapeComponent Component Properties
This component inherits the properties of
ezPxCharacterShapeComponent and adds these properties:
CapsuleRadius: The height and radius of the capsule shape. Note that the total height of the capsule character shape is
2 * radius + height.
ezPxCharacterControllerComponent Component Properties
ezPxCharacterControllerComponent expects to find a
ezPxCharacterShapeComponent attached to the very same game object. Note that this class is mainly intended for demonstration purposes and may not have all the features and behavior that you need in your game.
RotateSpeed: How fast the CC rotates around the up axis due to mouse or keyboard input.
AirSpeed: The speed at which the CC will walk, run, move while crouching, or be able to navigate while not touching the ground.
AirFriction: How much the CC will slow down when not touching the ground.
CrouchHeight: How tall the CC is during crouching. Note that the total height of the capsule character shape is
2 * radius + height.
JumpImpulse: With how much force the CC will jump, and consequently how high it will jump.
PushingForce: With how much force the CC will push dynamic actors when walking into them.
WalkSurfaceInteraction: The surface interaction to trigger on the surfaces that the CC walks over. This is typically used to create footstep sounds.
RunInteractionDistance: The distance that the CC has to walk or run, until another surface interaction is triggered.
FallbackWalkSurface: The surface to use for triggering footstep interactions, if the ground has no surface of its own.
HeadObject: An object reference to a child object of the CC, which is considered the ‘head’. If such a reference is set, the CC will move this object up and down when the CC crouches or stands up. The main camera component should be attached to this head object, such that the player sees the difference. This property is only useful for first-person player characters.