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 ¶
func (Instance) FloorConstantSpeed ¶
func (Instance) FloorMaxAngle ¶
func (Instance) FloorSnapLength ¶
func (Instance) FloorStopOnSlope ¶
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) MoreArgs ¶
MoreArgs enables certain functions to be called with additional 'optional' arguments.
func (Instance) MotionMode ¶
func (self Instance) MotionMode() MotionMode
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 ¶
func (Instance) PlatformOnLeave ¶
func (self Instance) PlatformOnLeave() PlatformOnLeave
func (Instance) PlatformWallLayers ¶
func (Instance) SafeMargin ¶
func (Instance) SetFloorBlockOnWall ¶
func (Instance) SetFloorConstantSpeed ¶
func (Instance) SetFloorMaxAngle ¶
func (Instance) SetFloorSnapLength ¶
func (Instance) SetFloorStopOnSlope ¶
func (Instance) SetMaxSlides ¶
func (Instance) SetMotionMode ¶
func (self Instance) SetMotionMode(value MotionMode)
func (Instance) SetPlatformFloorLayers ¶
func (Instance) SetPlatformOnLeave ¶
func (self Instance) SetPlatformOnLeave(value PlatformOnLeave)
func (Instance) SetPlatformWallLayers ¶
func (Instance) SetSafeMargin ¶
func (Instance) SetSlideOnCeiling ¶
func (Instance) SetUpDirection ¶
func (Instance) SetVelocity ¶
func (Instance) SetWallMinSlideAngle ¶
func (Instance) SlideOnCeiling ¶
func (Instance) UpDirection ¶
func (Instance) WallMinSlideAngle ¶
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 )