Documentation
¶
Overview ¶
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 PhysicsBody2D.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, AnimatableBody2D is simpler to configure.
Index ¶
- type Advanced
- type Any
- type Expanded
- type Extension
- func (self *Extension[T]) AsCanvasItem() CanvasItem.Instance
- func (self *Extension[T]) AsCharacterBody2D() Instance
- func (self *Extension[T]) AsCollisionObject2D() CollisionObject2D.Instance
- func (self *Extension[T]) AsNode() Node.Instance
- func (self *Extension[T]) AsNode2D() Node2D.Instance
- func (self *Extension[T]) AsObject() [1]gd.Object
- func (self *Extension[T]) AsPhysicsBody2D() PhysicsBody2D.Instance
- type ID
- type Instance
- func (self Instance) ApplyFloorSnap()
- func (self Instance) AsCanvasItem() CanvasItem.Instance
- func (self Instance) AsCharacterBody2D() Instance
- func (self Instance) AsCollisionObject2D() CollisionObject2D.Instance
- func (self Instance) AsNode() Node.Instance
- func (self Instance) AsNode2D() Node2D.Instance
- func (self Instance) AsObject() [1]gd.Object
- func (self Instance) AsPhysicsBody2D() PhysicsBody2D.Instance
- func (self Instance) FloorBlockOnWall() bool
- func (self Instance) FloorConstantSpeed() bool
- func (self Instance) FloorMaxAngle() Angle.Radians
- func (self Instance) FloorSnapLength() Float.X
- func (self Instance) FloorStopOnSlope() bool
- func (self Instance) GetFloorAngle() Angle.Radians
- func (self Instance) GetFloorNormal() Vector2.XY
- func (self Instance) GetLastMotion() Vector2.XY
- func (self Instance) GetLastSlideCollision() KinematicCollision2D.Instance
- func (self Instance) GetPlatformVelocity() Vector2.XY
- func (self Instance) GetPositionDelta() Vector2.XY
- func (self Instance) GetRealVelocity() Vector2.XY
- func (self Instance) GetSlideCollision(slide_idx int) KinematicCollision2D.Instance
- func (self Instance) GetSlideCollisionCount() int
- func (self Instance) GetWallNormal() Vector2.XY
- func (self Instance) ID() ID
- func (self Instance) IsOnCeiling() bool
- func (self Instance) IsOnCeilingOnly() bool
- func (self Instance) IsOnFloor() bool
- func (self Instance) IsOnFloorOnly() bool
- func (self Instance) IsOnWall() bool
- func (self Instance) IsOnWallOnly() bool
- func (self Instance) MaxSlides() int
- func (self Instance) MoreArgs() MoreArgs
- func (self Instance) MotionMode() MotionMode
- func (self Instance) MoveAndSlide() bool
- func (self Instance) PlatformFloorLayers() int
- func (self Instance) PlatformOnLeave() PlatformOnLeave
- func (self Instance) PlatformWallLayers() int
- func (self Instance) SafeMargin() Float.X
- func (self Instance) SetFloorBlockOnWall(value bool)
- func (self Instance) SetFloorConstantSpeed(value bool)
- func (self Instance) SetFloorMaxAngle(value Angle.Radians)
- func (self Instance) SetFloorSnapLength(value Float.X)
- func (self Instance) SetFloorStopOnSlope(value bool)
- func (self Instance) SetMaxSlides(value int)
- func (self Instance) SetMotionMode(value MotionMode)
- func (self *Instance) SetObject(obj [1]gd.Object) bool
- func (self Instance) SetPlatformFloorLayers(value int)
- func (self Instance) SetPlatformOnLeave(value PlatformOnLeave)
- func (self Instance) SetPlatformWallLayers(value int)
- func (self Instance) SetSafeMargin(value Float.X)
- func (self Instance) SetSlideOnCeiling(value bool)
- func (self Instance) SetUpDirection(value Vector2.XY)
- func (self Instance) SetVelocity(value Vector2.XY)
- func (self Instance) SetWallMinSlideAngle(value Angle.Radians)
- func (self Instance) SlideOnCeiling() bool
- func (self Instance) UpDirection() Vector2.XY
- func (self Instance) Velocity() Vector2.XY
- func (self Instance) Virtual(name string) reflect.Value
- func (self Instance) WallMinSlideAngle() Angle.Radians
- type MoreArgs
- type MotionMode
- type PlatformOnLeave
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 Extension ¶
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 (*Extension[T]) AsCollisionObject2D ¶
func (self *Extension[T]) AsCollisionObject2D() CollisionObject2D.Instance
func (*Extension[T]) AsPhysicsBody2D ¶
func (self *Extension[T]) AsPhysicsBody2D() PhysicsBody2D.Instance
type 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.
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 (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 IsOnFloor returns true.
func (Instance) AsCanvasItem ¶
func (self Instance) AsCanvasItem() CanvasItem.Instance
func (Instance) AsCharacterBody2D ¶
func (Instance) AsCollisionObject2D ¶
func (self Instance) AsCollisionObject2D() CollisionObject2D.Instance
func (Instance) AsPhysicsBody2D ¶
func (self Instance) AsPhysicsBody2D() PhysicsBody2D.Instance
func (Instance) FloorBlockOnWall ¶
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.
func (Instance) FloorConstantSpeed ¶
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 FloorSnapLength to stick along a downward slope at constant speed.
func (Instance) FloorMaxAngle ¶
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.
func (Instance) FloorSnapLength ¶
Sets a snapping distance. When set to a value different from 0.0, the body is kept attached to slopes when calling MoveAndSlide. The snapping vector is determined by the given distance along the opposite direction of the UpDirection.
As long as the snapping vector is in contact with the ground and the body moves against UpDirection, the body will remain attached to the surface. Snapping is not applied if the body moves along 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 ApplyFloorSnap.
func (Instance) FloorStopOnSlope ¶
If true, the body will not slide on slopes when calling MoveAndSlide when the body is standing still.
If false, the body will slide on floor's slopes when Velocity applies a downward force.
func (Instance) GetFloorAngle ¶
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 MoveAndSlide and when IsOnFloor returns true.
func (Instance) GetFloorNormal ¶
Returns the collision normal of the floor at the last collision point. Only valid after calling MoveAndSlide and when IsOnFloor returns true.
Warning: The collision normal is not always the same as the surface normal.
func (Instance) GetLastMotion ¶
Returns the last motion applied to the CharacterBody2D during the last call to 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 KinematicCollision2D, which contains information about the latest collision that occurred during the last call to MoveAndSlide.
func (Instance) GetPlatformVelocity ¶
Returns the linear velocity of the platform at the last collision point. Only valid after calling MoveAndSlide.
func (Instance) GetPositionDelta ¶
Returns the travel (position delta) that occurred during the last call to MoveAndSlide.
func (Instance) GetRealVelocity ¶
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 to Velocity which returns the requested velocity.
func (Instance) GetSlideCollision ¶
func (self Instance) GetSlideCollision(slide_idx int) KinematicCollision2D.Instance
Returns a KinematicCollision2D, which contains information about a collision that occurred during the last call to MoveAndSlide. Since the body can collide several times in a single call to MoveAndSlide, you must specify the index of the collision in the range 0 to (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 ¶
Returns the number of times the body collided and changed direction during the last call to MoveAndSlide.
func (Instance) GetWallNormal ¶
Returns the collision normal of the wall at the last collision point. Only valid after calling MoveAndSlide and when IsOnWall returns true.
Warning: The collision normal is not always the same as the surface normal.
func (Instance) IsOnCeiling ¶
Returns true if the body collided with the ceiling on the last call of MoveAndSlide. Otherwise, returns false. The UpDirection and FloorMaxAngle are used to determine whether a surface is "ceiling" or not.
func (Instance) IsOnCeilingOnly ¶
Returns true if the body collided only with the ceiling on the last call of MoveAndSlide. Otherwise, returns false. The UpDirection and FloorMaxAngle are used to determine whether a surface is "ceiling" or not.
func (Instance) IsOnFloor ¶
Returns true if the body collided with the floor on the last call of MoveAndSlide. Otherwise, returns false. The UpDirection and FloorMaxAngle are used to determine whether a surface is "floor" or not.
func (Instance) IsOnFloorOnly ¶
Returns true if the body collided only with the floor on the last call of MoveAndSlide. Otherwise, returns false. The UpDirection and FloorMaxAngle are used to determine whether a surface is "floor" or not.
func (Instance) IsOnWall ¶
Returns true if the body collided with a wall on the last call of MoveAndSlide. Otherwise, returns false. The UpDirection and FloorMaxAngle are used to determine whether a surface is "wall" or not.
func (Instance) IsOnWallOnly ¶
Returns true if the body collided only with a wall on the last call of MoveAndSlide. Otherwise, returns false. The UpDirection and FloorMaxAngle are used to determine whether a surface is "wall" or not.
func (Instance) MaxSlides ¶
Maximum number of times the body can change direction before it stops when calling MoveAndSlide. Must be greater than zero.
func (Instance) MoreArgs ¶
MoreArgs enables certain functions to be called with additional 'optional' arguments.
func (Instance) MotionMode ¶
func (self Instance) MotionMode() MotionMode
Sets the motion mode which defines the behavior of MoveAndSlide.
func (Instance) MoveAndSlide ¶
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 a CharacterBody2D or 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 Velocity if a slide collision occurred. To get the latest collision call GetLastSlideCollision, for detailed information about collisions that occurred, use 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 MotionMode.
Returns true if the body collided, otherwise, returns false.
func (Instance) PlatformFloorLayers ¶
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.
func (Instance) PlatformOnLeave ¶
func (self Instance) PlatformOnLeave() 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.
func (Instance) PlatformWallLayers ¶
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.
func (Instance) SafeMargin ¶
Extra margin used for collision recovery when calling 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.
func (Instance) SetFloorBlockOnWall ¶
SetFloorBlockOnWall sets the property returned by [IsFloorBlockOnWallEnabled].
func (Instance) SetFloorConstantSpeed ¶
SetFloorConstantSpeed sets the property returned by [IsFloorConstantSpeedEnabled].
func (Instance) SetFloorMaxAngle ¶
SetFloorMaxAngle sets the property returned by [GetFloorMaxAngle].
func (Instance) SetFloorSnapLength ¶
SetFloorSnapLength sets the property returned by [GetFloorSnapLength].
func (Instance) SetFloorStopOnSlope ¶
SetFloorStopOnSlope sets the property returned by [IsFloorStopOnSlopeEnabled].
func (Instance) SetMaxSlides ¶
SetMaxSlides sets the property returned by [GetMaxSlides].
func (Instance) SetMotionMode ¶
func (self Instance) SetMotionMode(value MotionMode)
SetMotionMode sets the property returned by [GetMotionMode].
func (Instance) SetPlatformFloorLayers ¶
SetPlatformFloorLayers sets the property returned by [GetPlatformFloorLayers].
func (Instance) SetPlatformOnLeave ¶
func (self Instance) SetPlatformOnLeave(value PlatformOnLeave)
SetPlatformOnLeave sets the property returned by [GetPlatformOnLeave].
func (Instance) SetPlatformWallLayers ¶
SetPlatformWallLayers sets the property returned by [GetPlatformWallLayers].
func (Instance) SetSafeMargin ¶
SetSafeMargin sets the property returned by [GetSafeMargin].
func (Instance) SetSlideOnCeiling ¶
SetSlideOnCeiling sets the property returned by [IsSlideOnCeilingEnabled].
func (Instance) SetUpDirection ¶
SetUpDirection sets the property returned by [GetUpDirection].
func (Instance) SetVelocity ¶
SetVelocity sets the property returned by [GetVelocity].
func (Instance) SetWallMinSlideAngle ¶
SetWallMinSlideAngle sets the property returned by [GetWallMinSlideAngle].
func (Instance) SlideOnCeiling ¶
If true, during a jump against the ceiling, the body will slide, if false it will be stopped and will fall vertically.
func (Instance) UpDirection ¶
Vector pointing upwards, used to determine what is a wall and what is a floor (or a ceiling) when calling 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 MotionModeFloating as MotionMode.
func (Instance) Velocity ¶
Current velocity vector in pixels per second, used and modified during calls to MoveAndSlide.
func (Instance) WallMinSlideAngle ¶
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 MotionMode is MotionModeFloating.
type MoreArgs ¶
type MoreArgs [1]gdclass.CharacterBody2D
MoreArgs is a container for Instance functions with additional 'optional' arguments.
func (MoreArgs) GetFloorAngle ¶
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 MoveAndSlide and when IsOnFloor returns true.
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 [Velocity] when you leave a moving platform. // // [Velocity]: https://pkg.go.dev/graphics.gd/classdb/#Instance.Velocity PlatformOnLeaveAddVelocity PlatformOnLeave = 0 // Add the last platform velocity to the [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. // // [Velocity]: https://pkg.go.dev/graphics.gd/classdb/#Instance.Velocity PlatformOnLeaveAddUpwardVelocity PlatformOnLeave = 1 // Do nothing when leaving a platform. PlatformOnLeaveDoNothing PlatformOnLeave = 2 )