CharacterBody2D
A code-controlled 2D character body.
CharacterBody2D 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
CharacterBody2DConstructor
public CharacterBody2D();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 CharacterBody2D.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 CharacterBody2D.MoveAndSlide. The snapping vector is determined by the given distance along the opposite direction of the CharacterBody2D.UpDirection.
As long as the snapping vector is in contact with the ground and the body moves against CharacterBody2D.UpDirection, the body will remain attached to the surface. Snapping is not applied if the body moves along CharacterBody2D.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 CharacterBody2D.ApplyFloorSnap.
FloorStopOnSlopeProperty
public bool FloorStopOnSlope { get; set; }If true, the body will not slide on slopes when calling CharacterBody2D.MoveAndSlide when the body is standing still.
If false, the body will slide on floor's slopes when CharacterBody2D.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 CharacterBody2D.MoveAndSlide. Must be greater than zero.
MotionModeProperty
public CharacterBody2D.MotionModeEnum MotionMode { get; set; }Sets the motion mode which defines the behavior of CharacterBody2D.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 CharacterBody2D. By default, all floor bodies are detected and propagate their velocity.
PlatformOnLeaveProperty
public CharacterBody2D.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 CharacterBody2D. By default, all wall bodies are ignored.
SafeMarginProperty
public float SafeMargin { get; set; }Extra margin used for collision recovery when calling CharacterBody2D.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 Vector2 UpDirection { get; set; }Vector pointing upwards, used to determine what is a wall and what is a floor (or a ceiling) when calling CharacterBody2D.MoveAndSlide. Defaults to Vector2.Up. As the vector will be normalized it can't be equal to Vector2.Zero, if you want all collisions to be reported as walls, consider using CharacterBody2D.MotionModeEnum.Floating as CharacterBody2D.MotionMode.
VelocityProperty
public Vector2 Velocity { get; set; }Requested/movement-adjusted velocity in units per second. MoveAndSlide integrates it over time; it is not exact world displacement for the last frame or complete platform velocity.
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. This property only affects movement when CharacterBody2D.MotionMode is CharacterBody2D.MotionModeEnum.Floating.
Methods48
ApplyFloorSnapvoid
public void ApplyFloorSnap();Allows to manually apply a snap to the floor regardless of the body's velocity. This function does nothing when CharacterBody2D.IsOnFloor returns true.
GetFloorAnglefloat
public float GetFloorAngle(Vector2? upDirection = default);Returns the floor's collision angle at the last collision point according to CharacterBody2D.UpDirection, which is Vector2.Up by default. This value is always positive and only valid after calling CharacterBody2D.MoveAndSlide and when CharacterBody2D.IsOnFloor returns true.
| Name | Type | Default |
|---|---|---|
upDirection | Vector2? | default |
upDirectionIf the parameter is null, then the default value is new Vector2(0.0f, -1.0f).
floatGetFloorMaxAnglefloat
public float GetFloorMaxAngle();floatGetFloorNormalVector2
public Vector2 GetFloorNormal();Returns the collision normal of the floor at the last collision point. Only valid after calling CharacterBody2D.MoveAndSlide and when CharacterBody2D.IsOnFloor returns true.
Warning: The collision normal is not always the same as the surface normal.
Vector2GetFloorSnapLengthfloat
public float GetFloorSnapLength();floatGetLastMotionVector2
public Vector2 GetLastMotion();Returns the last motion applied to the CharacterBody2D during the last call to CharacterBody2D.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.
Vector2GetLastSlideCollisionKinematicCollision2D
public KinematicCollision2D GetLastSlideCollision();Returns a KinematicCollision2D if a collision occurred. The returned value contains information about the latest collision that occurred during the last call to CharacterBody2D.MoveAndSlide. Returns null if no collision occurred. See also CharacterBody2D.GetSlideCollision.
KinematicCollision2DGetMaxSlidesint
public int GetMaxSlides();intGetMotionModeCharacterBody2D.MotionModeEnum
public CharacterBody2D.MotionModeEnum GetMotionMode();CharacterBody2D.MotionModeEnumGetPlatformFloorLayersuint
public uint GetPlatformFloorLayers();uintGetPlatformOnLeaveCharacterBody2D.PlatformOnLeaveEnum
public CharacterBody2D.PlatformOnLeaveEnum GetPlatformOnLeave();CharacterBody2D.PlatformOnLeaveEnumGetPlatformVelocityVector2
public Vector2 GetPlatformVelocity();Returns the linear velocity of the platform at the last collision point. Only valid after calling CharacterBody2D.MoveAndSlide.
Vector2GetPlatformWallLayersuint
public uint GetPlatformWallLayers();uintGetPositionDeltaVector2
public Vector2 GetPositionDelta();Returns the travel (position delta) that occurred during the last call to CharacterBody2D.MoveAndSlide.
Vector2GetRealVelocityVector2
public Vector2 GetRealVelocity();Returns the current real velocity since the last call to CharacterBody2D.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 CharacterBody2D.Velocity which returns the requested velocity.
Vector2GetSafeMarginfloat
public float GetSafeMargin();floatGetSlideCollisionKinematicCollision2D
public KinematicCollision2D GetSlideCollision(int slideIdx);Returns a KinematicCollision2D, which contains information about a collision that occurred during the last call to CharacterBody2D.MoveAndSlide. Since the body can collide several times in a single call to CharacterBody2D.MoveAndSlide, you must specify the index of the collision in the range 0 to (CharacterBody2D.GetSlideCollisionCount - 1). See also CharacterBody2D.GetLastSlideCollision.
Example: Iterate through the collisions with a for loop:
for (int i = 0; i < GetSlideCollisionCount(); i++)
{
KinematicCollision2D collision = GetSlideCollision(i);
ReckfellApi.Print("Collided with: ", (collision.GetCollider() as Entity).Name);
}| Name | Type | Default |
|---|---|---|
slideIdx | int | — |
KinematicCollision2DGetSlideCollisionCountint
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.
intGetUpDirectionVector2
public Vector2 GetUpDirection();Vector2GetVelocityVector2
public Vector2 GetVelocity();Vector2GetWallMinSlideAnglefloat
public float GetWallMinSlideAngle();floatGetWallNormalVector2
public Vector2 GetWallNormal();Returns the collision normal of the wall at the last collision point. Only valid after calling CharacterBody2D.MoveAndSlide and when CharacterBody2D.IsOnWall returns true.
Warning: The collision normal is not always the same as the surface normal.
Vector2IsFloorBlockOnWallEnabledbool
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 CharacterBody2D.MoveAndSlide. Otherwise, returns false. The CharacterBody2D.UpDirection and CharacterBody2D.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 CharacterBody2D.MoveAndSlide. Otherwise, returns false. The CharacterBody2D.UpDirection and CharacterBody2D.FloorMaxAngle are used to determine whether a surface is "ceiling" or not.
boolIsOnFloorbool
public bool IsOnFloor();Floor state after the last MoveAndSlide relative to UpDirection/FloorMaxAngle. Before initial movement or after teleporting, old state is not a fresh support check.
boolIsOnFloorOnlybool
public bool IsOnFloorOnly();Returns true if the body collided only with the floor on the last call of CharacterBody2D.MoveAndSlide. Otherwise, returns false. The CharacterBody2D.UpDirection and CharacterBody2D.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 CharacterBody2D.MoveAndSlide. Otherwise, returns false. The CharacterBody2D.UpDirection and CharacterBody2D.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 CharacterBody2D.MoveAndSlide. Otherwise, returns false. The CharacterBody2D.UpDirection and CharacterBody2D.FloorMaxAngle are used to determine whether a surface is "wall" or not.
boolIsSlideOnCeilingEnabledbool
public bool IsSlideOnCeilingEnabled();boolMoveAndSlidebool
public bool MoveAndSlide();Moves using Velocity with timestep integration and sliding. Do not multiply Velocity by delta before calling. Returns a collision bool, refreshes contact state and may modify Velocity.
boolSetFloorBlockOnWallEnabledvoid
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(CharacterBody2D.MotionModeEnum mode);| Name | Type | Default |
|---|---|---|
mode | CharacterBody2D.MotionModeEnum | — |
SetPlatformFloorLayersvoid
public void SetPlatformFloorLayers(uint excludeLayer);| Name | Type | Default |
|---|---|---|
excludeLayer | uint | — |
SetPlatformOnLeavevoid
public void SetPlatformOnLeave(CharacterBody2D.PlatformOnLeaveEnum onLeaveApplyVelocity);| Name | Type | Default |
|---|---|---|
onLeaveApplyVelocity | CharacterBody2D.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(Vector2 upDirection);| Name | Type | Default |
|---|---|---|
upDirection | Vector2 | — |
SetVelocityvoid
public void SetVelocity(Vector2 velocity);| Name | Type | Default |
|---|---|---|
velocity | Vector2 | — |
SetWallMinSlideAnglevoid
public void SetWallMinSlideAngle(float radians);| Name | Type | Default |
|---|---|---|
radians | float | — |
CharacterBody2D.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 sided games like platformers. |
CharacterBody2D.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. |