CharacterBody3D
A code-controlled 3D character body.
CharacterBody3D moves through MoveAndSlide or MoveAndCollide. The script defines acceleration, input and jump rules; the type alone does not add controls or gravity.
Set Velocity in units/s before MoveAndSlide without multiplying by delta again. MoveAndCollide accepts displacement for the call. IsOnFloor/IsOnWall and slide contacts describe the last MoveAndSlide, not persistent proximity. After teleporting, do not authorize an action using old floor state. Grounded/Floating, UpDirection, snap and maximum-angle settings serve separate roles.
Declarations checked against C# SDK 1.0.0. Expand a name to see declarations and documentation.
Constructors1
CharacterBody3DConstructor
public CharacterBody3D();Properties15
FloorBlockOnWallProperty
public bool FloorBlockOnWall { get; set; }If true, the body will be able to move on the floor only. This option avoids to be able to walk on walls, it will however allow to slide down along them.
FloorConstantSpeedProperty
public bool FloorConstantSpeed { get; set; }If false (by default), the body will move faster on downward slopes and slower on upward slopes.
If true, the body will always move at the same speed on the ground no matter the slope. Note that you need to use CharacterBody3D.FloorSnapLength to stick along a downward slope at constant speed.
FloorMaxAngleProperty
public float FloorMaxAngle { get; set; }The floor/ceiling classification angle relative to UpDirection, in radians. Use Mathf.DegToRad(45) for 45 degrees.
FloorSnapLengthProperty
public float FloorSnapLength { get; set; }Sets a snapping distance. When set to a value different from 0.0, the body is kept attached to slopes when calling CharacterBody3D.MoveAndSlide. The snapping vector is determined by the given distance along the opposite direction of the CharacterBody3D.UpDirection.
As long as the snapping vector is in contact with the ground and the body moves against CharacterBody3D.UpDirection, the body will remain attached to the surface. Snapping is not applied if the body moves along CharacterBody3D.UpDirection, meaning it contains vertical rising velocity, so it will be able to detach from the ground when jumping or when the body is pushed up by something. If you want to apply a snap without taking into account the velocity, use CharacterBody3D.ApplyFloorSnap.
FloorStopOnSlopeProperty
public bool FloorStopOnSlope { get; set; }If true, the body will not slide on slopes when calling CharacterBody3D.MoveAndSlide when the body is standing still.
If false, the body will slide on floor's slopes when CharacterBody3D.Velocity applies a downward force.
MaxSlidesProperty
public int MaxSlides { get; set; }Maximum number of times the body can change direction before it stops when calling CharacterBody3D.MoveAndSlide. Must be greater than zero.
MotionModeProperty
public CharacterBody3D.MotionModeEnum MotionMode { get; set; }Sets the motion mode which defines the behavior of CharacterBody3D.MoveAndSlide.
PlatformFloorLayersProperty
public uint PlatformFloorLayers { get; set; }Collision layers that will be included for detecting floor bodies that will act as moving platforms to be followed by the CharacterBody3D. By default, all floor bodies are detected and propagate their velocity.
PlatformOnLeaveProperty
public CharacterBody3D.PlatformOnLeaveEnum PlatformOnLeave { get; set; }Sets the behavior to apply when you leave a moving platform. By default, to be physically accurate, when you leave the last platform velocity is applied.
PlatformWallLayersProperty
public uint PlatformWallLayers { get; set; }Collision layers that will be included for detecting wall bodies that will act as moving platforms to be followed by the CharacterBody3D. By default, all wall bodies are ignored.
SafeMarginProperty
public float SafeMargin { get; set; }Extra margin used for collision recovery when calling CharacterBody3D.MoveAndSlide.
If the body is at least this close to another body, it will consider them to be colliding and will be pushed away before performing the actual motion.
A higher value means it's more flexible for detecting collision, which helps with consistently detecting walls and floors.
A lower value forces the collision algorithm to use more exact detection, so it can be used in cases that specifically require precision, e.g at very low scale to avoid visible jittering, or for stability with a stack of character bodies.
SlideOnCeilingProperty
public bool SlideOnCeiling { get; set; }If true, during a jump against the ceiling, the body will slide, if false it will be stopped and will fall vertically.
UpDirectionProperty
public Vector3 UpDirection { get; set; }Vector pointing upwards, used to determine what is a wall and what is a floor (or a ceiling) when calling CharacterBody3D.MoveAndSlide. Defaults to Vector3.Up. As the vector will be normalized it can't be equal to Vector3.Zero, if you want all collisions to be reported as walls, consider using CharacterBody3D.MotionModeEnum.Floating as CharacterBody3D.MotionMode.
VelocityProperty
public Vector3 Velocity { get; set; }Body velocity in units per second for MoveAndSlide. Do not multiply by delta before calling: movement integrates the step internally. MoveAndSlide may modify velocity during collisions.
WallMinSlideAngleProperty
public float WallMinSlideAngle { get; set; }Minimum angle (in radians) where the body is allowed to slide when it encounters a wall. The default value equals 15 degrees. When CharacterBody3D.MotionMode is CharacterBody3D.MotionModeEnum.Grounded, it only affects movement if CharacterBody3D.FloorBlockOnWall is true.
Methods49
ApplyFloorSnapvoid
public void ApplyFloorSnap();Allows to manually apply a snap to the floor regardless of the body's velocity. This function does nothing when CharacterBody3D.IsOnFloor returns true.
GetFloorAnglefloat
public float GetFloorAngle(Vector3? upDirection = default);Returns the floor's collision angle at the last collision point according to CharacterBody3D.UpDirection, which is Vector3.Up by default. This value is always positive and only valid after calling CharacterBody3D.MoveAndSlide and when CharacterBody3D.IsOnFloor returns true.
| Name | Type | Default |
|---|---|---|
upDirection | Vector3? | default |
upDirectionIf the parameter is null, then the default value is new Vector3(0.0f, 1.0f, 0.0f).
floatGetFloorMaxAnglefloat
public float GetFloorMaxAngle();floatGetFloorNormalVector3
public Vector3 GetFloorNormal();Returns the collision normal of the floor at the last collision point. Only valid after calling CharacterBody3D.MoveAndSlide and when CharacterBody3D.IsOnFloor returns true.
Warning: The collision normal is not always the same as the surface normal.
Vector3GetFloorSnapLengthfloat
public float GetFloorSnapLength();floatGetLastMotionVector3
public Vector3 GetLastMotion();Returns the last motion applied to the CharacterBody3D during the last call to CharacterBody3D.MoveAndSlide. The movement can be split into multiple motions when sliding occurs, and this method return the last one, which is useful to retrieve the current direction of the movement.
Vector3GetLastSlideCollisionKinematicCollision3D
public KinematicCollision3D GetLastSlideCollision();Returns a KinematicCollision3D if a collision occurred. The returned value contains information about the latest collision that occurred during the last call to CharacterBody3D.MoveAndSlide. Returns null if no collision occurred. See also CharacterBody3D.GetSlideCollision.
KinematicCollision3DGetMaxSlidesint
public int GetMaxSlides();intGetMotionModeCharacterBody3D.MotionModeEnum
public CharacterBody3D.MotionModeEnum GetMotionMode();CharacterBody3D.MotionModeEnumGetPlatformAngularVelocityVector3
public Vector3 GetPlatformAngularVelocity();Returns the angular velocity of the platform at the last collision point. Only valid after calling CharacterBody3D.MoveAndSlide.
Vector3GetPlatformFloorLayersuint
public uint GetPlatformFloorLayers();uintGetPlatformOnLeaveCharacterBody3D.PlatformOnLeaveEnum
public CharacterBody3D.PlatformOnLeaveEnum GetPlatformOnLeave();CharacterBody3D.PlatformOnLeaveEnumGetPlatformVelocityVector3
public Vector3 GetPlatformVelocity();Returns the linear velocity of the platform at the last collision point. Only valid after calling CharacterBody3D.MoveAndSlide.
Vector3GetPlatformWallLayersuint
public uint GetPlatformWallLayers();uintGetPositionDeltaVector3
public Vector3 GetPositionDelta();Returns the travel (position delta) that occurred during the last call to CharacterBody3D.MoveAndSlide.
Vector3GetRealVelocityVector3
public Vector3 GetRealVelocity();Returns the current real velocity since the last call to CharacterBody3D.MoveAndSlide. For example, when you climb a slope, you will move diagonally even though the velocity is horizontal. This method returns the diagonal movement, as opposed to CharacterBody3D.Velocity which returns the requested velocity.
Vector3GetSafeMarginfloat
public float GetSafeMargin();floatGetSlideCollisionKinematicCollision3D
public KinematicCollision3D GetSlideCollision(int slideIdx);Returns a KinematicCollision3D, which contains information about a collision that occurred during the last call to CharacterBody3D.MoveAndSlide. Since the body can collide several times in a single call to CharacterBody3D.MoveAndSlide, you must specify the index of the collision in the range 0 to (CharacterBody3D.GetSlideCollisionCount - 1). See also CharacterBody3D.GetLastSlideCollision.
| Name | Type | Default |
|---|---|---|
slideIdx | int | — |
KinematicCollision3DGetSlideCollisionCountint
public int GetSlideCollisionCount();The number of slide contacts from the last MoveAndSlide. Not a count of every neighboring body; read MoveAndCollide data from its returned object.
intGetUpDirectionVector3
public Vector3 GetUpDirection();Vector3GetVelocityVector3
public Vector3 GetVelocity();Vector3GetWallMinSlideAnglefloat
public float GetWallMinSlideAngle();floatGetWallNormalVector3
public Vector3 GetWallNormal();Returns the collision normal of the wall at the last collision point. Only valid after calling CharacterBody3D.MoveAndSlide and when CharacterBody3D.IsOnWall returns true.
Warning: The collision normal is not always the same as the surface normal.
Vector3IsFloorBlockOnWallEnabledbool
public bool IsFloorBlockOnWallEnabled();boolIsFloorConstantSpeedEnabledbool
public bool IsFloorConstantSpeedEnabled();boolIsFloorStopOnSlopeEnabledbool
public bool IsFloorStopOnSlopeEnabled();boolIsOnCeilingbool
public bool IsOnCeiling();Returns true if the body collided with the ceiling on the last call of CharacterBody3D.MoveAndSlide. Otherwise, returns false. The CharacterBody3D.UpDirection and CharacterBody3D.FloorMaxAngle are used to determine whether a surface is "ceiling" or not.
boolIsOnCeilingOnlybool
public bool IsOnCeilingOnly();Returns true if the body collided only with the ceiling on the last call of CharacterBody3D.MoveAndSlide. Otherwise, returns false. The CharacterBody3D.UpDirection and CharacterBody3D.FloorMaxAngle are used to determine whether a surface is "ceiling" or not.
boolIsOnFloorbool
public bool IsOnFloor();Reads floor-contact state produced by movement processing. Use after a physics step, rather than as a NavigationMesh-presence test.
booltrue for a detected floor contact. A navigation mesh without a physics surface does not create that contact.
IsOnFloorOnlybool
public bool IsOnFloorOnly();Returns true if the body collided only with the floor on the last call of CharacterBody3D.MoveAndSlide. Otherwise, returns false. The CharacterBody3D.UpDirection and CharacterBody3D.FloorMaxAngle are used to determine whether a surface is "floor" or not.
boolIsOnWallbool
public bool IsOnWall();Returns true if the body collided with a wall on the last call of CharacterBody3D.MoveAndSlide. Otherwise, returns false. The CharacterBody3D.UpDirection and CharacterBody3D.FloorMaxAngle are used to determine whether a surface is "wall" or not.
boolIsOnWallOnlybool
public bool IsOnWallOnly();Returns true if the body collided only with a wall on the last call of CharacterBody3D.MoveAndSlide. Otherwise, returns false. The CharacterBody3D.UpDirection and CharacterBody3D.FloorMaxAngle are used to determine whether a surface is "wall" or not.
boolIsSlideOnCeilingEnabledbool
public bool IsSlideOnCeilingEnabled();boolMoveAndSlidebool
public bool MoveAndSlide();Moves the body using Velocity and collision response. Uses the engine time step; your code supplies velocity and gravity acceleration. Does not search navigation paths or establish goal arrival.
booltrue when movement registered collisions; not an indication of successful navigation completion.
SetFloorBlockOnWallEnabledvoid
public void SetFloorBlockOnWallEnabled(bool enabled);| Name | Type | Default |
|---|---|---|
enabled | bool | — |
SetFloorConstantSpeedEnabledvoid
public void SetFloorConstantSpeedEnabled(bool enabled);| Name | Type | Default |
|---|---|---|
enabled | bool | — |
SetFloorMaxAnglevoid
public void SetFloorMaxAngle(float radians);| Name | Type | Default |
|---|---|---|
radians | float | — |
SetFloorSnapLengthvoid
public void SetFloorSnapLength(float floorSnapLength);| Name | Type | Default |
|---|---|---|
floorSnapLength | float | — |
SetFloorStopOnSlopeEnabledvoid
public void SetFloorStopOnSlopeEnabled(bool enabled);| Name | Type | Default |
|---|---|---|
enabled | bool | — |
SetMaxSlidesvoid
public void SetMaxSlides(int maxSlides);| Name | Type | Default |
|---|---|---|
maxSlides | int | — |
SetMotionModevoid
public void SetMotionMode(CharacterBody3D.MotionModeEnum mode);| Name | Type | Default |
|---|---|---|
mode | CharacterBody3D.MotionModeEnum | — |
SetPlatformFloorLayersvoid
public void SetPlatformFloorLayers(uint excludeLayer);| Name | Type | Default |
|---|---|---|
excludeLayer | uint | — |
SetPlatformOnLeavevoid
public void SetPlatformOnLeave(CharacterBody3D.PlatformOnLeaveEnum onLeaveApplyVelocity);| Name | Type | Default |
|---|---|---|
onLeaveApplyVelocity | CharacterBody3D.PlatformOnLeaveEnum | — |
SetPlatformWallLayersvoid
public void SetPlatformWallLayers(uint excludeLayer);| Name | Type | Default |
|---|---|---|
excludeLayer | uint | — |
SetSafeMarginvoid
public void SetSafeMargin(float margin);| Name | Type | Default |
|---|---|---|
margin | float | — |
SetSlideOnCeilingEnabledvoid
public void SetSlideOnCeilingEnabled(bool enabled);| Name | Type | Default |
|---|---|---|
enabled | bool | — |
SetUpDirectionvoid
public void SetUpDirection(Vector3 upDirection);| Name | Type | Default |
|---|---|---|
upDirection | Vector3 | — |
SetVelocityvoid
public void SetVelocity(Vector3 velocity);| Name | Type | Default |
|---|---|---|
velocity | Vector3 | — |
SetWallMinSlideAnglevoid
public void SetWallMinSlideAngle(float radians);| Name | Type | Default |
|---|---|---|
radians | float | — |
CharacterBody3D.MotionModeEnum
| Value | Description |
|---|---|
Floating = 1 | Apply when there is no notion of floor or ceiling. All collisions will be reported as |
Grounded = 0 | Apply when notions of walls, ceiling and floor are relevant. In this mode the body motion will react to slopes (acceleration/slowdown). This mode is suitable for grounded games like platformers. |
CharacterBody3D.PlatformOnLeaveEnum
| Value | Description |
|---|---|
AddUpwardVelocity = 1 | Add the last platform velocity to the |
AddVelocity = 0 | Add the last platform velocity to the |
DoNothing = 2 | Do nothing when leaving a platform. |