reckfellENGINE

CharacterBody2D

A code-controlled 2D character body.

On this page
class CharacterBody2DReckfellSharpInherits PhysicsBody2D

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.

Working examples.

Declarations checked against C# SDK 1.0.0. Expand a name to see declarations and documentation.

Constructors1

CharacterBody2DConstructor
# CharacterBody2D
C#
public CharacterBody2D();

Properties15

FloorBlockOnWallProperty
# FloorBlockOnWall
C#
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
# FloorConstantSpeed
C#
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
# FloorMaxAngle
C#
public float FloorMaxAngle { get; set; }

The floor/ceiling classification angle relative to UpDirection, in radians. Use Mathf.DegToRad(45) for 45 degrees.

FloorSnapLengthProperty
# FloorSnapLength
C#
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
# FloorStopOnSlope
C#
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
# MaxSlides
C#
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
# MotionMode
C#
public CharacterBody2D.MotionModeEnum MotionMode { get; set; }

Sets the motion mode which defines the behavior of CharacterBody2D.MoveAndSlide.

PlatformFloorLayersProperty
# PlatformFloorLayers
C#
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
# PlatformOnLeave
C#
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
# PlatformWallLayers
C#
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
# SafeMargin
C#
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
# SlideOnCeiling
C#
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
# UpDirection
C#
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
# Velocity
C#
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
# WallMinSlideAngle
C#
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
# ApplyFloorSnap
C#
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
# GetFloorAngle
C#
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.

Parameters
NameTypeDefault
upDirectionVector2?default
upDirection

If the parameter is null, then the default value is new Vector2(0.0f, -1.0f).

Returns float
GetFloorMaxAnglefloat
# GetFloorMaxAngle
C#
public float GetFloorMaxAngle();
Returns float
GetFloorNormalVector2
# GetFloorNormal
C#
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.

Returns Vector2
GetFloorSnapLengthfloat
# GetFloorSnapLength
C#
public float GetFloorSnapLength();
Returns float
GetLastMotionVector2
# GetLastMotion
C#
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.

Returns Vector2
GetLastSlideCollisionKinematicCollision2D
# GetLastSlideCollision
C#
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.

Returns KinematicCollision2D
GetMaxSlidesint
# GetMaxSlides
C#
public int GetMaxSlides();
Returns int
GetMotionModeCharacterBody2D.MotionModeEnum
# GetMotionMode
C#
public CharacterBody2D.MotionModeEnum GetMotionMode();
Returns CharacterBody2D.MotionModeEnum
GetPlatformFloorLayersuint
# GetPlatformFloorLayers
C#
public uint GetPlatformFloorLayers();
Returns uint
GetPlatformOnLeaveCharacterBody2D.PlatformOnLeaveEnum
# GetPlatformOnLeave
C#
public CharacterBody2D.PlatformOnLeaveEnum GetPlatformOnLeave();
Returns CharacterBody2D.PlatformOnLeaveEnum
GetPlatformVelocityVector2
# GetPlatformVelocity
C#
public Vector2 GetPlatformVelocity();

Returns the linear velocity of the platform at the last collision point. Only valid after calling CharacterBody2D.MoveAndSlide.

Returns Vector2
GetPlatformWallLayersuint
# GetPlatformWallLayers
C#
public uint GetPlatformWallLayers();
Returns uint
GetPositionDeltaVector2
# GetPositionDelta
C#
public Vector2 GetPositionDelta();

Returns the travel (position delta) that occurred during the last call to CharacterBody2D.MoveAndSlide.

Returns Vector2
GetRealVelocityVector2
# GetRealVelocity
C#
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.

Returns Vector2
GetSafeMarginfloat
# GetSafeMargin
C#
public float GetSafeMargin();
Returns float
GetSlideCollisionKinematicCollision2D
# GetSlideCollision
C#
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:

C#
for (int i = 0; i < GetSlideCollisionCount(); i++)
{
    KinematicCollision2D collision = GetSlideCollision(i);
    ReckfellApi.Print("Collided with: ", (collision.GetCollider() as Entity).Name);
}
Parameters
NameTypeDefault
slideIdxint—
Returns KinematicCollision2D
GetSlideCollisionCountint
# GetSlideCollisionCount
C#
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.

Returns int
GetUpDirectionVector2
# GetUpDirection
C#
public Vector2 GetUpDirection();
Returns Vector2
GetVelocityVector2
# GetVelocity
C#
public Vector2 GetVelocity();
Returns Vector2
GetWallMinSlideAnglefloat
# GetWallMinSlideAngle
C#
public float GetWallMinSlideAngle();
Returns float
GetWallNormalVector2
# GetWallNormal
C#
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.

Returns Vector2
IsFloorBlockOnWallEnabledbool
# IsFloorBlockOnWallEnabled
C#
public bool IsFloorBlockOnWallEnabled();
Returns bool
IsFloorConstantSpeedEnabledbool
# IsFloorConstantSpeedEnabled
C#
public bool IsFloorConstantSpeedEnabled();
Returns bool
IsFloorStopOnSlopeEnabledbool
# IsFloorStopOnSlopeEnabled
C#
public bool IsFloorStopOnSlopeEnabled();
Returns bool
IsOnCeilingbool
# IsOnCeiling
C#
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.

Returns bool
IsOnCeilingOnlybool
# IsOnCeilingOnly
C#
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.

Returns bool
IsOnFloorbool
# IsOnFloor
C#
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.

Returns bool
IsOnFloorOnlybool
# IsOnFloorOnly
C#
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.

Returns bool
IsOnWallbool
# IsOnWall
C#
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.

Returns bool
IsOnWallOnlybool
# IsOnWallOnly
C#
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.

Returns bool
IsSlideOnCeilingEnabledbool
# IsSlideOnCeilingEnabled
C#
public bool IsSlideOnCeilingEnabled();
Returns bool
MoveAndSlidebool
# MoveAndSlide
C#
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.

Returns bool
SetFloorBlockOnWallEnabledvoid
# SetFloorBlockOnWallEnabled
C#
public void SetFloorBlockOnWallEnabled(bool enabled);
Parameters
NameTypeDefault
enabledbool—
SetFloorConstantSpeedEnabledvoid
# SetFloorConstantSpeedEnabled
C#
public void SetFloorConstantSpeedEnabled(bool enabled);
Parameters
NameTypeDefault
enabledbool—
SetFloorMaxAnglevoid
# SetFloorMaxAngle
C#
public void SetFloorMaxAngle(float radians);
Parameters
NameTypeDefault
radiansfloat—
SetFloorSnapLengthvoid
# SetFloorSnapLength
C#
public void SetFloorSnapLength(float floorSnapLength);
Parameters
NameTypeDefault
floorSnapLengthfloat—
SetFloorStopOnSlopeEnabledvoid
# SetFloorStopOnSlopeEnabled
C#
public void SetFloorStopOnSlopeEnabled(bool enabled);
Parameters
NameTypeDefault
enabledbool—
SetMaxSlidesvoid
# SetMaxSlides
C#
public void SetMaxSlides(int maxSlides);
Parameters
NameTypeDefault
maxSlidesint—
SetMotionModevoid
# SetMotionMode
C#
public void SetMotionMode(CharacterBody2D.MotionModeEnum mode);
Parameters
NameTypeDefault
modeCharacterBody2D.MotionModeEnum—
SetPlatformFloorLayersvoid
# SetPlatformFloorLayers
C#
public void SetPlatformFloorLayers(uint excludeLayer);
Parameters
NameTypeDefault
excludeLayeruint—
SetPlatformOnLeavevoid
# SetPlatformOnLeave
C#
public void SetPlatformOnLeave(CharacterBody2D.PlatformOnLeaveEnum onLeaveApplyVelocity);
Parameters
NameTypeDefault
onLeaveApplyVelocityCharacterBody2D.PlatformOnLeaveEnum—
SetPlatformWallLayersvoid
# SetPlatformWallLayers
C#
public void SetPlatformWallLayers(uint excludeLayer);
Parameters
NameTypeDefault
excludeLayeruint—
SetSafeMarginvoid
# SetSafeMargin
C#
public void SetSafeMargin(float margin);
Parameters
NameTypeDefault
marginfloat—
SetSlideOnCeilingEnabledvoid
# SetSlideOnCeilingEnabled
C#
public void SetSlideOnCeilingEnabled(bool enabled);
Parameters
NameTypeDefault
enabledbool—
SetUpDirectionvoid
# SetUpDirection
C#
public void SetUpDirection(Vector2 upDirection);
Parameters
NameTypeDefault
upDirectionVector2—
SetVelocityvoid
# SetVelocity
C#
public void SetVelocity(Vector2 velocity);
Parameters
NameTypeDefault
velocityVector2—
SetWallMinSlideAnglevoid
# SetWallMinSlideAngle
C#
public void SetWallMinSlideAngle(float radians);
Parameters
NameTypeDefault
radiansfloat—

CharacterBody2D.MotionModeEnum

ValueDescription
Floating = 1

Apply when there is no notion of floor or ceiling. All collisions will be reported as on_wall. In this mode, when you slide, the speed will always be constant. This mode is suitable for top-down games.

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

ValueDescription
AddUpwardVelocity = 1

Add the last platform velocity to the CharacterBody2D.Velocity when you leave a moving platform, but any downward motion is ignored. It's useful to keep full jump height even when the platform is moving down.

AddVelocity = 0

Add the last platform velocity to the CharacterBody2D.Velocity when you leave a moving platform.

DoNothing = 2

Do nothing when leaving a platform.

Reckfell Engine documentationBack to top ↑
QUICK LINKSGetting startedEntity hierarchyC# & .NET 10Scenes and prefabs
Reckfell guides and full API K
reckfellENGINE
RECKFELL ACCOUNT

Sign in

Sign in to your Reckfell account.

One account for the entire Reckfell platform.