Motion 2D
Moves a kinematic Body2D by casting its colliders: walking, jumping, slopes, steps and moving platforms, or free top-down movement. Call Move and Jump from a script; the controller acts on the next fixed step.
component gameplay.Motion2D
Usage
Needs Rigid Body 2D on the same node.
Try it
Limit movement speed
A script continuously requests rightward movement. At Max Speed 0 the kinematic body cannot move, and the marker stays orange.
Set Max Speed to 6. Motion 2D accelerates and moves the actual body; the marker turns green when its runtime velocity is positive. Reset stops it again.
Scene2 nodes
Scriptmain.aes
package demo
// The script requests movement, but the running Motion2D system computes the
// speed and position. The color reports its actual velocity, not MaxSpeed.
script MotionIndicator {
func FixedUpdate(dt float) {
m := node.Get<Motion2D>()
m.Move(1)
if m.Velocity.X > 0.1 {
node.Get<SpriteRenderer>().Tint = Color{0.05, 0.8, 0.25, 1}
} else {
node.Get<SpriteRenderer>().Tint = Color{1, 0.5, 0.05, 1}
}
}
}
Fields
| Name | Type | Default | Description |
|---|---|---|---|
| Mode | enum | platformer | platformer walks on floors under gravity; topDown moves freely in the plane. one of: platformer, topDown |
| Up Direction | Vec2 | (0, -1) | Up for floors, jumps and gravity. (0, -1) is up on screen. |
| Max Speed | float | 6 | Top speed in units per second. ≥ 0 |
| Accel | float | 60 | How fast the speed grows towards the target, units per second squared. ≥ 0 |
| Decel | float | 50 | How fast the speed falls without input, in units per second squared. ≥ 0 |
| Air Control | float | 0.6 | Share of Accel and Decel available in the air: 0 cannot steer, 1 steers as on the ground. 0 – 1 |
| Gravity Scale | float | 1 | Multiplies the physics gravity; 0 turns gravity off for the character. |
| Max Fall Speed | float | 20 | Fastest fall in units per second. 0 is unlimited. ≥ 0 |
| Jump Height | float | 2.2 | Height of a full jump in units; sets the jump speed unless Jump Speed is set. ≥ 0 |
| Jump Speed | float | 0 | Upward speed of a jump. 0 derives it from Jump Height and gravity. ≥ 0 |
| Jump Cut Multiplier | float | 0.5 | Upward speed is multiplied by this when the jump button is released early. 0 – 1 |
| Coyote Time | float | 0.1 | Seconds after leaving a ledge during which a jump still works. ≥ 0 |
| Jump Buffer | float | 0.1 | Seconds a jump pressed in the air is remembered until landing. ≥ 0 |
| Max Slope | float | 45 | Steepest floor in degrees; steeper surfaces are walls. 0 – 89 |
| Step Height | float | 0.2 | Steps up to this height, in units, are climbed without jumping. 0 never steps up. ≥ 0 |
| Snap To Ground | float | 0.2 | Keeps the character on slopes and steps down by up to this distance, in units. 0 never snaps. ≥ 0 |
| Safe Margin | float | 0.02 | Gap in units kept to surfaces so casts never start inside them. ≥ 0.001 |
| Max Slides | int | 4 | How many surfaces one step may slide along. 1 – 8 |
| Collision Mask | layer mask | -1 | Layers the character collides with. mask over physics.layers |
| Push Bodies | bool | true | Moves through dynamic bodies and pushes them instead of stopping at them. |
| Moving Platforms | bool | true | Carries the character with the body it stands on. |
| Platform On Leave | enum | keep | keep keeps the platform's velocity after leaving it, drop loses it, add keeps only its horizontal part. one of: keep, drop, add |
| Face Movement | bool | false | topDown: turns the node towards the movement. |
| Dash Gravity | bool | false | Applies gravity during a dash. |
| Ledge Climb | float | 0 | platformer: in the air, moving into a wall whose top is at most this far above the feet climbs onto it. 0 never climbs. ≥ 0 |
| Climb Time | float | 0.3 | Seconds a ledge climb takes: rising, then stepping onto the ledge. ≥ 0 |
Runtime state
Values the component has while the game runs. They are not saved with the scene.
| Name | Type | Default | Description |
|---|---|---|---|
| Velocity | Vec2 | (0, 0) | Velocity in units per second. |
| Grounded | bool | false | Whether the character stands on a floor. |
| On Wall | bool | false | Whether the character touched a wall in the last step. |
| On Ceiling | bool | false | Whether the character touched a ceiling in the last step. |
| Wall Normal | Vec2 | (0, 0) | Normal of the last wall touched. |
| Floor Normal | Vec2 | (0, 0) | Normal of the floor under the character. |
| Floor Angle | float | 0 | Slope of the floor in degrees. |
| Ground Body | NodeRef | none | Body the character stands on. |
| Climbing | bool | false | Whether the character is climbing onto a ledge. |
Methods
AddDisplacement method
func (Motion2D) AddDisplacement(offset Vec2)
Moves by an offset in world units on the next step, through walls and slopes like a move, without changing the velocity; root motion of an Animator uses it.
AddVelocity method
func (Motion2D) AddVelocity(velocity Vec2)
Adds a push once, for knockback.
Dash method
func (Motion2D) Dash(velocity Vec2, seconds float)
Moves at a fixed velocity for some seconds.
Jump method
func (Motion2D) Jump()
Jumps on the next step if grounded, within coyote time or later within the jump buffer.
JumpReleased method
func (Motion2D) JumpReleased()
Cuts a rising jump by Jump Cut Multiplier.
Move method
func (Motion2D) Move(x float)
Sets the horizontal input, -1..1; it stays until the next Move.
MoveDir method
func (Motion2D) MoveDir(dir Vec2)
Sets the top-down input; a longer vector is cut to length 1.
Stop method
func (Motion2D) Stop()
Zeroes the velocity and ends a dash.
Teleport method
func (Motion2D) Teleport(position Vec2)
Moves to a position on the next step and resets velocity and state.
Signals
Climbed signal
signal Climbed()
Fires when the character starts climbing onto a ledge.
HitCeiling signal
signal HitCeiling()
Fires when the character bumps into a ceiling.
HitWall signal
signal HitWall(normal Vec2)
Fires when the character starts touching a wall.
Jumped signal
signal Jumped()
Fires when a jump starts.
Landed signal
signal Landed(speed float)
Fires when the character touches a floor, with the falling speed.
LeftGround signal
signal LeftGround()
Fires when the character walks off a floor without jumping.
Related guides
- Platformer from scratch This guide builds a small platformer in an empty 2D project: a character that runs and jumps, a camera that follows it, a pit with a checkpoint, coins and an enemy.
- Recipes Short answers to common platformer questions.
- Time and authority A player in game time advances on fixed steps and stops while the game is paused.