CharacterBody2D
A 2D physics body specialized for characters moved by script.
CharacterBody2D.swift:14class CharacterBody2D
CharacterBody2D
is a specialized class for physics bodies that are meant to be user-controlled. They are not affected by physics at all, but they affect other physics bodies in their path. They are mainly used to provide high-level API to move objects with wall and slope detection (moveAndSlide
method) in addition to the general collision detection provided by moveAndCollide(motion:testOnly:safeMargin:recoveryAsCollision:)
. This makes it useful for highly configurable physics bodies that must move in specific ways and collide with the world, as is often the case with user-controlled characters.
For game objects that don’t require complex movement or collision detection, such as moving platforms, AnimatableBody2D
is simpler to configure.
Superclasses
class PhysicsBody2D
Abstract base class for 2D game objects affected by physics.
Citizens in SwiftGodot
Conformances
protocol CustomStringConvertible
A type with a customized textual representation.
protocol Equatable
A type that can be compared for value equality.
protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.protocol Identifiable<ID>
A class of types whose instances hold the value of an entity with stable identity.
protocol VariantRepresentable
Types that conform to VariantRepresentable can be stored directly in
Variant
with no conversion. These include all of the Variant types from Godot (for exampleGString
,Rect
,Plane
), Godot objects (those that subclass SwiftGodot.Object) as well as the built-in Swift types UInt8, Int64 and Double.protocol VariantStorable
Types that conform to VariantStorable can be stored in a Variant and can be extracted back out of a Variant.
Types
Type members
Instance members
var floorBlockOnWall: Bool
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.var floorConstantSpeed: Bool
If
false
(by default), the body will move faster on downward slopes and slower on upward slopes.var floorMaxAngle: Double
Maximum angle (in radians) where a slope is still considered a floor (or a ceiling), rather than a wall, when calling
moveAndSlide
. The default value equals 45 degrees.var floorSnapLength: Double
Sets a snapping distance. When set to a value different from
0.0
, the body is kept attached to slopes when callingmoveAndSlide
. The snapping vector is determined by the given distance along the opposite direction of theupDirection
.var floorStopOnSlope: Bool
If
true
, the body will not slide on slopes when callingmoveAndSlide
when the body is standing still.var maxSlides: Int32
Maximum number of times the body can change direction before it stops when calling
moveAndSlide
.var motionMode: CharacterBody2D.MotionMode
Sets the motion mode which defines the behavior of
moveAndSlide
. SeeMotionMode
constants for available modes.var platformFloorLayers: UInt32
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.var platformOnLeave: CharacterBody2D.PlatformOnLeave
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. See
PlatformOnLeave
constants for available behavior.var platformWallLayers: UInt32
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.var safeMargin: Double
Extra margin used for collision recovery when calling
moveAndSlide
.var slideOnCeiling: Bool
If
true
, during a jump against the ceiling, the body will slide, iffalse
it will be stopped and will fall vertically.var upDirection: Vector2
Vector pointing upwards, used to determine what is a wall and what is a floor (or a ceiling) when calling
moveAndSlide
. Defaults toVector2.UP
. As the vector will be normalized it can’t be equal toVector2.ZERO
, if you want all collisions to be reported as walls, consider using .floating asmotionMode
.var velocity: Vector2
Current velocity vector in pixels per second, used and modified during calls to
moveAndSlide
.var wallMinSlideAngle: Double
Minimum angle (in radians) where the body is allowed to slide when it encounters a slope. The default value equals 15 degrees. This property only affects movement when
motionMode
is .floating.func applyFloorSnap(
) Allows to manually apply a snap to the floor regardless of the body’s velocity. This function does nothing when
isOnFloor
returnstrue
.func getFloorAngle(upDirection: Vector2
) -> Double Returns the floor’s collision angle at the last collision point according to
upDirection
, which isVector2.UP
by default. This value is always positive and only valid after callingmoveAndSlide
and whenisOnFloor
returnstrue
.func getFloorNormal(
) -> Vector2 Returns the surface normal of the floor at the last collision point. Only valid after calling
moveAndSlide
and whenisOnFloor
returnstrue
.func getLastMotion(
) -> Vector2 Returns the last motion applied to the
CharacterBody2D
during the last call tomoveAndSlide
. 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.func getLastSlideCollision(
) -> KinematicCollision2D? Returns a
KinematicCollision2D
, which contains information about the latest collision that occurred during the last call tomoveAndSlide
.func getPlatformVelocity(
) -> Vector2 Returns the linear velocity of the platform at the last collision point. Only valid after calling
moveAndSlide
.func getPositionDelta(
) -> Vector2 Returns the travel (position delta) that occurred during the last call to
moveAndSlide
.func getRealVelocity(
) -> Vector2 Returns the current real velocity since the last call to
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 tovelocity
which returns the requested velocity.func getSlideCollision(slideIdx: Int32
) -> KinematicCollision2D? Returns a
KinematicCollision2D
, which contains information about a collision that occurred during the last call tomoveAndSlide
. Since the body can collide several times in a single call tomoveAndSlide
, you must specify the index of the collision in the range 0 to (getSlideCollisionCount
- 1).func getSlideCollisionCount(
) -> Int32 Returns the number of times the body collided and changed direction during the last call to
moveAndSlide
.func getWallNormal(
) -> Vector2 Returns the surface normal of the wall at the last collision point. Only valid after calling
moveAndSlide
and whenisOnWall
returnstrue
.func isOnCeiling(
) -> Bool Returns
true
if the body collided with the ceiling on the last call ofmoveAndSlide
. Otherwise, returnsfalse
. TheupDirection
andfloorMaxAngle
are used to determine whether a surface is “ceiling” or not.func isOnCeilingOnly(
) -> Bool Returns
true
if the body collided only with the ceiling on the last call ofmoveAndSlide
. Otherwise, returnsfalse
. TheupDirection
andfloorMaxAngle
are used to determine whether a surface is “ceiling” or not.func isOnFloor(
) -> Bool Returns
true
if the body collided with the floor on the last call ofmoveAndSlide
. Otherwise, returnsfalse
. TheupDirection
andfloorMaxAngle
are used to determine whether a surface is “floor” or not.func isOnFloorOnly(
) -> Bool Returns
true
if the body collided only with the floor on the last call ofmoveAndSlide
. Otherwise, returnsfalse
. TheupDirection
andfloorMaxAngle
are used to determine whether a surface is “floor” or not.func isOnWall(
) -> Bool Returns
true
if the body collided with a wall on the last call ofmoveAndSlide
. Otherwise, returnsfalse
. TheupDirection
andfloorMaxAngle
are used to determine whether a surface is “wall” or not.func isOnWallOnly(
) -> Bool Returns
true
if the body collided only with a wall on the last call ofmoveAndSlide
. Otherwise, returnsfalse
. TheupDirection
andfloorMaxAngle
are used to determine whether a surface is “wall” or not.func moveAndSlide(
) -> Bool Moves the body based on
velocity
. If the body collides with another, it will slide along the other body (by default only on floor) rather than stop immediately. If the other body is aCharacterBody2D
orRigidBody2D
, it will also be affected by the motion of the other body. You can use this to make moving and rotating platforms, or to make nodes push other nodes.