CharacterBody2D

package
v0.0.0-...-c858641 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 14, 2025 License: MIT Imports: 29 Imported by: 0

Documentation

Overview

graphics.gd/classdb/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 (Instance.MoveAndSlide method) in addition to the general collision detection provided by graphics.gd/classdb/PhysicsBody2D.Instance.MoveAndCollide. 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, graphics.gd/classdb/AnimatableBody2D is simpler to configure.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Advanced

type Advanced = class

Advanced exposes a 1:1 low-level instance of the class, undocumented, for those who know what they are doing.

type Any

type Any interface {
	gd.IsClass
	AsCharacterBody2D() Instance
}

type Expanded

type Expanded [1]gdclass.CharacterBody2D

func (Expanded) GetFloorAngle

func (self Expanded) GetFloorAngle(up_direction Vector2.XY) Angle.Radians

Returns the floor's collision angle at the last collision point according to 'up_direction', which is [Vector2.Up] by default. This value is always positive and only valid after calling Instance.MoveAndSlide and when Instance.IsOnFloor returns true.

type Extension

type Extension[T gdclass.Interface] struct{ gdclass.Extension[T, Instance] }

Extension can be embedded in a new struct to create an extension of this class. T should be the type that is embedding this Extension

func (*Extension[T]) AsCanvasItem

func (self *Extension[T]) AsCanvasItem() CanvasItem.Instance

func (*Extension[T]) AsCharacterBody2D

func (self *Extension[T]) AsCharacterBody2D() Instance

func (*Extension[T]) AsCollisionObject2D

func (self *Extension[T]) AsCollisionObject2D() CollisionObject2D.Instance

func (*Extension[T]) AsNode

func (self *Extension[T]) AsNode() Node.Instance

func (*Extension[T]) AsNode2D

func (self *Extension[T]) AsNode2D() Node2D.Instance

func (*Extension[T]) AsObject

func (self *Extension[T]) AsObject() [1]gd.Object

func (*Extension[T]) AsPhysicsBody2D

func (self *Extension[T]) AsPhysicsBody2D() PhysicsBody2D.Instance

type ID

type ID Object.ID

ID is a typed object ID (reference) to an instance of this class, use it to store references to objects with unknown lifetimes, as an ID will not panic on use if the underlying object has been destroyed.

func (ID) Instance

func (id ID) Instance() (Instance, bool)

type Instance

type Instance [1]gdclass.CharacterBody2D

Instance of the class with convieniently typed arguments and results.

var Nil Instance

Nil is a nil/null instance of the class. Equivalent to the zero value.

func New

func New() Instance

func (Instance) ApplyFloorSnap

func (self Instance) ApplyFloorSnap()

Allows to manually apply a snap to the floor regardless of the body's velocity. This function does nothing when Instance.IsOnFloor returns true.

func (Instance) AsCanvasItem

func (self Instance) AsCanvasItem() CanvasItem.Instance

func (Instance) AsCharacterBody2D

func (self Instance) AsCharacterBody2D() Instance

func (Instance) AsCollisionObject2D

func (self Instance) AsCollisionObject2D() CollisionObject2D.Instance

func (Instance) AsNode

func (self Instance) AsNode() Node.Instance

func (Instance) AsNode2D

func (self Instance) AsNode2D() Node2D.Instance

func (Instance) AsObject

func (self Instance) AsObject() [1]gd.Object

func (Instance) AsPhysicsBody2D

func (self Instance) AsPhysicsBody2D() PhysicsBody2D.Instance

func (Instance) FloorBlockOnWall

func (self Instance) FloorBlockOnWall() bool

func (Instance) FloorConstantSpeed

func (self Instance) FloorConstantSpeed() bool

func (Instance) FloorMaxAngle

func (self Instance) FloorMaxAngle() Angle.Radians

func (Instance) FloorSnapLength

func (self Instance) FloorSnapLength() Float.X

func (Instance) FloorStopOnSlope

func (self Instance) FloorStopOnSlope() bool

func (Instance) GetFloorAngle

func (self Instance) GetFloorAngle() Angle.Radians

Returns the floor's collision angle at the last collision point according to 'up_direction', which is [Vector2.Up] by default. This value is always positive and only valid after calling Instance.MoveAndSlide and when Instance.IsOnFloor returns true.

func (Instance) GetFloorNormal

func (self Instance) GetFloorNormal() Vector2.XY

Returns the collision normal of the floor at the last collision point. Only valid after calling Instance.MoveAndSlide and when Instance.IsOnFloor returns true.

Warning: The collision normal is not always the same as the surface normal.

func (Instance) GetLastMotion

func (self Instance) GetLastMotion() Vector2.XY

Returns the last motion applied to the graphics.gd/classdb/CharacterBody2D during the last call to Instance.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.

func (Instance) GetLastSlideCollision

func (self Instance) GetLastSlideCollision() KinematicCollision2D.Instance

Returns a graphics.gd/classdb/KinematicCollision2D, which contains information about the latest collision that occurred during the last call to Instance.MoveAndSlide.

func (Instance) GetPlatformVelocity

func (self Instance) GetPlatformVelocity() Vector2.XY

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

func (Instance) GetPositionDelta

func (self Instance) GetPositionDelta() Vector2.XY

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

func (Instance) GetRealVelocity

func (self Instance) GetRealVelocity() Vector2.XY

Returns the current real velocity since the last call to Instance.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 Instance.Velocity which returns the requested velocity.

func (Instance) GetSlideCollision

func (self Instance) GetSlideCollision(slide_idx int) KinematicCollision2D.Instance

Returns a graphics.gd/classdb/KinematicCollision2D, which contains information about a collision that occurred during the last call to Instance.MoveAndSlide. Since the body can collide several times in a single call to Instance.MoveAndSlide, you must specify the index of the collision in the range 0 to (Instance.GetSlideCollisionCount - 1).

Example: Iterate through the collisions with a for loop:

for i := range characterBody2D.GetSlideCollisionCount() {
	var collision = characterBody2D.GetSlideCollision(i)
	fmt.Println("Collided with: ", Object.To[Node.Instance](collision.GetCollider()).Name())
}

func (Instance) GetSlideCollisionCount

func (self Instance) GetSlideCollisionCount() int

Returns the number of times the body collided and changed direction during the last call to Instance.MoveAndSlide.

func (Instance) GetWallNormal

func (self Instance) GetWallNormal() Vector2.XY

Returns the collision normal of the wall at the last collision point. Only valid after calling Instance.MoveAndSlide and when Instance.IsOnWall returns true.

Warning: The collision normal is not always the same as the surface normal.

func (Instance) ID

func (self Instance) ID() ID

func (Instance) IsOnCeiling

func (self Instance) IsOnCeiling() bool

Returns true if the body collided with the ceiling on the last call of Instance.MoveAndSlide. Otherwise, returns false. The Instance.UpDirection and Instance.FloorMaxAngle are used to determine whether a surface is "ceiling" or not.

func (Instance) IsOnCeilingOnly

func (self Instance) IsOnCeilingOnly() bool

Returns true if the body collided only with the ceiling on the last call of Instance.MoveAndSlide. Otherwise, returns false. The Instance.UpDirection and Instance.FloorMaxAngle are used to determine whether a surface is "ceiling" or not.

func (Instance) IsOnFloor

func (self Instance) IsOnFloor() bool

Returns true if the body collided with the floor on the last call of Instance.MoveAndSlide. Otherwise, returns false. The Instance.UpDirection and Instance.FloorMaxAngle are used to determine whether a surface is "floor" or not.

func (Instance) IsOnFloorOnly

func (self Instance) IsOnFloorOnly() bool

Returns true if the body collided only with the floor on the last call of Instance.MoveAndSlide. Otherwise, returns false. The Instance.UpDirection and Instance.FloorMaxAngle are used to determine whether a surface is "floor" or not.

func (Instance) IsOnWall

func (self Instance) IsOnWall() bool

Returns true if the body collided with a wall on the last call of Instance.MoveAndSlide. Otherwise, returns false. The Instance.UpDirection and Instance.FloorMaxAngle are used to determine whether a surface is "wall" or not.

func (Instance) IsOnWallOnly

func (self Instance) IsOnWallOnly() bool

Returns true if the body collided only with a wall on the last call of Instance.MoveAndSlide. Otherwise, returns false. The Instance.UpDirection and Instance.FloorMaxAngle are used to determine whether a surface is "wall" or not.

func (Instance) MaxSlides

func (self Instance) MaxSlides() int

func (Instance) MotionMode

func (self Instance) MotionMode() MotionMode

func (Instance) MoveAndSlide

func (self Instance) MoveAndSlide() bool

Moves the body based on Instance.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 a graphics.gd/classdb/CharacterBody2D or graphics.gd/classdb/RigidBody2D, 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.

Modifies Instance.Velocity if a slide collision occurred. To get the latest collision call Instance.GetLastSlideCollision, for detailed information about collisions that occurred, use Instance.GetSlideCollision.

When the body touches a moving platform, the platform's velocity is automatically added to the body motion. If a collision occurs due to the platform's motion, it will always be first in the slide collisions.

The general behavior and available properties change according to the Instance.MotionMode.

Returns true if the body collided, otherwise, returns false.

func (Instance) PlatformFloorLayers

func (self Instance) PlatformFloorLayers() int

func (Instance) PlatformOnLeave

func (self Instance) PlatformOnLeave() PlatformOnLeave

func (Instance) PlatformWallLayers

func (self Instance) PlatformWallLayers() int

func (Instance) SafeMargin

func (self Instance) SafeMargin() Float.X

func (Instance) SetFloorBlockOnWall

func (self Instance) SetFloorBlockOnWall(value bool)

func (Instance) SetFloorConstantSpeed

func (self Instance) SetFloorConstantSpeed(value bool)

func (Instance) SetFloorMaxAngle

func (self Instance) SetFloorMaxAngle(value Angle.Radians)

func (Instance) SetFloorSnapLength

func (self Instance) SetFloorSnapLength(value Float.X)

func (Instance) SetFloorStopOnSlope

func (self Instance) SetFloorStopOnSlope(value bool)

func (Instance) SetMaxSlides

func (self Instance) SetMaxSlides(value int)

func (Instance) SetMotionMode

func (self Instance) SetMotionMode(value MotionMode)

func (*Instance) SetObject

func (self *Instance) SetObject(obj [1]gd.Object) bool

func (Instance) SetPlatformFloorLayers

func (self Instance) SetPlatformFloorLayers(value int)

func (Instance) SetPlatformOnLeave

func (self Instance) SetPlatformOnLeave(value PlatformOnLeave)

func (Instance) SetPlatformWallLayers

func (self Instance) SetPlatformWallLayers(value int)

func (Instance) SetSafeMargin

func (self Instance) SetSafeMargin(value Float.X)

func (Instance) SetSlideOnCeiling

func (self Instance) SetSlideOnCeiling(value bool)

func (Instance) SetUpDirection

func (self Instance) SetUpDirection(value Vector2.XY)

func (Instance) SetVelocity

func (self Instance) SetVelocity(value Vector2.XY)

func (Instance) SetWallMinSlideAngle

func (self Instance) SetWallMinSlideAngle(value Angle.Radians)

func (Instance) SlideOnCeiling

func (self Instance) SlideOnCeiling() bool

func (Instance) UpDirection

func (self Instance) UpDirection() Vector2.XY

func (Instance) Velocity

func (self Instance) Velocity() Vector2.XY

func (Instance) Virtual

func (self Instance) Virtual(name string) reflect.Value

func (Instance) WallMinSlideAngle

func (self Instance) WallMinSlideAngle() Angle.Radians

type MotionMode

type MotionMode int //gd:CharacterBody2D.MotionMode
const (
	// 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.
	MotionModeGrounded MotionMode = 0
	// 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.
	MotionModeFloating MotionMode = 1
)

type PlatformOnLeave

type PlatformOnLeave int //gd:CharacterBody2D.PlatformOnLeave
const (
	// Add the last platform velocity to the [Instance.Velocity] when you leave a moving platform.
	PlatformOnLeaveAddVelocity PlatformOnLeave = 0
	// Add the last platform velocity to the [Instance.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.
	PlatformOnLeaveAddUpwardVelocity PlatformOnLeave = 1
	// Do nothing when leaving a platform.
	PlatformOnLeaveDoNothing PlatformOnLeave = 2
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL