Aego Русский

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
On this page

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.

Starts the scene; open the example parameters to experiment.
Open in AegoDownload example project
Scene2 nodes
Motion marker
Transform
position[0, 0]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")tintColor#f59e0bffsizeVec2[3, 2]
Rigid Body 2D
typeenumkinematicfixedRotationbooltrueinterpolateboolfalse
Motion 2D
modeenumtopDownmaxSpeedfloat0accelfloat240decelfloat240collisionMasklayer mask-1
Camera
Transform
position[0, 0]rotation0scale[1, 1]
Camera 2D
zoomfloat40clearColorColor#111827ffscaleenumfitVirtualvirtualVec2[960, 540]activebooltrue
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

NameTypeDefaultDescription
Modeenumplatformer

platformer walks on floors under gravity; topDown moves freely in the plane.

one of: platformer, topDown
Up DirectionVec2(0, -1)

Up for floors, jumps and gravity. (0, -1) is up on screen.

Max Speedfloat6

Top speed in units per second.

≥ 0
Accelfloat60

How fast the speed grows towards the target, units per second squared.

≥ 0
Decelfloat50

How fast the speed falls without input, in units per second squared.

≥ 0
Air Controlfloat0.6

Share of Accel and Decel available in the air: 0 cannot steer, 1 steers as on the ground.

0 – 1
Gravity Scalefloat1

Multiplies the physics gravity; 0 turns gravity off for the character.

Max Fall Speedfloat20

Fastest fall in units per second. 0 is unlimited.

≥ 0
Jump Heightfloat2.2

Height of a full jump in units; sets the jump speed unless Jump Speed is set.

≥ 0
Jump Speedfloat0

Upward speed of a jump. 0 derives it from Jump Height and gravity.

≥ 0
Jump Cut Multiplierfloat0.5

Upward speed is multiplied by this when the jump button is released early.

0 – 1
Coyote Timefloat0.1

Seconds after leaving a ledge during which a jump still works.

≥ 0
Jump Bufferfloat0.1

Seconds a jump pressed in the air is remembered until landing.

≥ 0
Max Slopefloat45

Steepest floor in degrees; steeper surfaces are walls.

0 – 89
Step Heightfloat0.2

Steps up to this height, in units, are climbed without jumping. 0 never steps up.

≥ 0
Snap To Groundfloat0.2

Keeps the character on slopes and steps down by up to this distance, in units. 0 never snaps.

≥ 0
Safe Marginfloat0.02

Gap in units kept to surfaces so casts never start inside them.

≥ 0.001
Max Slidesint4

How many surfaces one step may slide along.

1 – 8
Collision Masklayer mask-1

Layers the character collides with.

mask over physics.layers
Push Bodiesbooltrue

Moves through dynamic bodies and pushes them instead of stopping at them.

Moving Platformsbooltrue

Carries the character with the body it stands on.

Platform On Leaveenumkeep

keep keeps the platform's velocity after leaving it, drop loses it, add keeps only its horizontal part.

one of: keep, drop, add
Face Movementboolfalse

topDown: turns the node towards the movement.

Dash Gravityboolfalse

Applies gravity during a dash.

Ledge Climbfloat0

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 Timefloat0.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.

NameTypeDefaultDescription
VelocityVec2(0, 0)

Velocity in units per second.

Groundedboolfalse

Whether the character stands on a floor.

On Wallboolfalse

Whether the character touched a wall in the last step.

On Ceilingboolfalse

Whether the character touched a ceiling in the last step.

Wall NormalVec2(0, 0)

Normal of the last wall touched.

Floor NormalVec2(0, 0)

Normal of the floor under the character.

Floor Anglefloat0

Slope of the floor in degrees.

Ground BodyNodeRefnone

Body the character stands on.

Climbingboolfalse

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.