Aego Русский

Character motion

Motion 2D moves a character with a kinematic Rigid Body 2D. Each fixed step it casts the body's colliders along the move, slides along what it hits and moves the body to the result, so the character never sinks into walls and still pushes crates.

Try walking with A/D or the arrow keys and jumping with Space, W or Up; the lift, slope and walls use the real physics system.

m := node.Get<Motion2D>()
m.Move(input.Axis("Move"))
if input.Pressed("Jump") {
    m.Jump()
}
if input.Released("Jump") {
    m.JumpReleased()
}

Move stays until the next call; Jump is one request that the controller keeps for Jump Buffer seconds, so a jump pressed shortly before landing still happens.

Mode Behaviour
platformer walks on floors under gravity, jumps, climbs steps and slopes
topDown moves freely in the plane with MoveDir, slides along walls; Face Movement turns the node

Motion 2D during Play: the Runtime fields show velocity, Grounded and the contacts

On this page

Floors, slopes and steps

Floors, walls and ceilings are told apart by the angle of the surface to Up Direction: a surface steeper than Max Slope is a wall. On a floor the character follows its slope without sliding down, and Snap To Ground keeps it on the floor when walking down a slope or a small step. Steps up to Step Height are climbed without jumping. Safe Margin is the gap kept to every surface so the next cast never starts inside one.

In the air a wall stops the movement into it and never lifts the character: a capsule or circle that meets the corner of a ledge slides off it instead of hanging there.

Ledges

With Ledge Climb above 0, a character in the air that moves into a wall whose top is at most Ledge Climb above its feet climbs onto it: it rises to the top, steps onto the ledge and lands there, over Climb Time seconds. Higher walls are left alone. The climb runs on fixed steps like the rest of the motion: input, gravity and root motion are ignored during it, and the velocity stays at zero. Knockback (AddVelocity), Stop, Dash and Teleport end it at once.

Climbing is true during the climb and Climbed fires when it starts. To animate it, pass the flag to the Animator in FixedUpdate, as the player of the platformer example does:

a := node.Get<Animator>()
a.SetBool("Climbing", m.Climbing)

and give the controller a Climb state entered from any while Climbing is true.

Jumps

Set Jump Height and the controller works out the jump speed from it and gravity; Jump Speed sets the speed directly instead. Releasing the button while rising cuts the jump short by Jump Cut Multiplier, so a tap gives a small hop.

Two windows make jumping forgiving. Coyote Time lets a jump work for a moment after walking off a ledge. Jump Buffer keeps a jump pressed in the air until the character lands. Air Control decides how much of the ground acceleration is available in the air.

Every field is listed on the Motion 2D page.

Platforms and bodies

With Moving Platforms the character rides on the body it stands on; Platform On Leave decides what happens to the platform's speed after a jump: keep adds it, drop loses it, add keeps only its horizontal part. With Push Bodies dynamic bodies do not stop the character, it pushes them.

Dash(velocity, seconds), AddVelocity for knockback, AddDisplacement(offset) for root motion and scripted nudges, Stop and Teleport cover the rest. A displacement moves the character through walls and slopes like a move, on the next step, and leaves the velocity alone. The Runtime fields show the velocity, whether the character is grounded or touching a wall or ceiling, the floor normal and angle and the body it stands on.

Try it

Change the example parameters and compare the result. Reset restores the initial scene.

Starts the scene; open the example parameters to experiment.
Open in AegoDownload example project
Scene7 nodes
Floor
Transform
position[0, 3]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint0tintColor#4b5563fforiginVec2[0.5, 0.5]sizeVec2[40, 1]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Rigid Body 2D
typeenumstaticmassModeenumautomassfloat0gravityScalefloat1linearDampingfloat0angularDampingfloat0fixedRotationboolfalsebulletboolfalseallowSleepbooltrueinterpolateboolfalselayerlayer0masklayer mask-1
Box Collider 2D
offsetVec2[0, 0]isTriggerboolfalsefrictionfloat0.6restitutionfloat0densityfloat1sizeVec2[40, 1]
Slope
Transform
position[9, 1.6]rotation-0.5236scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint0tintColor#6b7280fforiginVec2[0.5, 0.5]sizeVec2[6, 1]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Rigid Body 2D
typeenumstaticmassModeenumautomassfloat0gravityScalefloat1linearDampingfloat0angularDampingfloat0fixedRotationboolfalsebulletboolfalseallowSleepbooltrueinterpolateboolfalselayerlayer0masklayer mask-1
Box Collider 2D
offsetVec2[0, 0]isTriggerboolfalsefrictionfloat0.6restitutionfloat0densityfloat1sizeVec2[6, 1]
Step
Transform
position[-4, 2.4]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint0tintColor#6b7280fforiginVec2[0.5, 0.5]sizeVec2[3, 0.2]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Rigid Body 2D
typeenumstaticmassModeenumautomassfloat0gravityScalefloat1linearDampingfloat0angularDampingfloat0fixedRotationboolfalsebulletboolfalseallowSleepbooltrueinterpolateboolfalselayerlayer0masklayer mask-1
Box Collider 2D
offsetVec2[0, 0]isTriggerboolfalsefrictionfloat0.6restitutionfloat0densityfloat1sizeVec2[3, 0.2]
Wall
Transform
position[-12, 0]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint0tintColor#4b5563fforiginVec2[0.5, 0.5]sizeVec2[1, 6]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Rigid Body 2D
typeenumstaticmassModeenumautomassfloat0gravityScalefloat1linearDampingfloat0angularDampingfloat0fixedRotationboolfalsebulletboolfalseallowSleepbooltrueinterpolateboolfalselayerlayer0masklayer mask-1
Box Collider 2D
offsetVec2[0, 0]isTriggerboolfalsefrictionfloat0.6restitutionfloat0densityfloat1sizeVec2[1, 6]
Lift
Transform
position[15, 2.3]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint0tintColor#0ea5e9fforiginVec2[0.5, 0.5]sizeVec2[3, 0.4]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Rigid Body 2D
typeenumkinematicmassModeenumautomassfloat0gravityScalefloat1linearDampingfloat0angularDampingfloat0fixedRotationboolfalsebulletboolfalseallowSleepboolfalseinterpolatebooltruelayerlayer0masklayer mask-1
Box Collider 2D
offsetVec2[0, 0]isTriggerboolfalsefrictionfloat0.6restitutionfloat0densityfloat1sizeVec2[3, 0.4]
Player
Transform
position[0, 0]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint1tintColor#f59e0bfforiginVec2[0.5, 0.5]sizeVec2[0.6, 1]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Rigid Body 2D
typeenumkinematicmassModeenumautomassfloat0gravityScalefloat1linearDampingfloat0angularDampingfloat0fixedRotationbooltruebulletboolfalseallowSleepboolfalseinterpolatebooltruelayerlayer0masklayer mask-1
Box Collider 2D
offsetVec2[0, 0]isTriggerboolfalsefrictionfloat0restitutionfloat0densityfloat1sizeVec2[0.6, 1]
Motion 2D
modeenumplatformerupDirectionVec2[0, -1]maxSpeedfloat6accelfloat60decelfloat50airControlfloat0.6gravityScalefloat1maxFallSpeedfloat20jumpHeightfloat2.2jumpSpeedfloat0jumpCutMultiplierfloat0.5coyoteTimefloat0.1jumpBufferfloat0.1maxSlopefloat45stepHeightfloat0.25snapToGroundfloat0.2safeMarginfloat0.02maxSlidesint4collisionMasklayer mask-1pushBodiesbooltruemovingPlatformsbooltrueplatformOnLeaveenumkeepfaceMovementboolfalsedashGravityboolfalse
Camera
Transform
position[0, 0]rotation0scale[1, 1]
Camera 2D
zoomfloat36viewportRect[0, 0, 1, 1]maskint-1orderint0clearbooltrueclearColorColor#111827ffscaleenumfitVirtualvirtualVec2[960, 540]activebooltrue
Virtual Camera 2D
priorityint0outputstring""zoomfloat0blendTimefloat0.5blendCurveCurve{ ease=inOutQuad }timeDomainenumunscaleduseBoundsboolfalseboundsRect[0, 0, 0, 0]snapToPixelsboolfalse
Camera Follow 2D
targetNodeRefnode("d6100000000000000000000000000006")deadzoneRect[0.4, 0.4, 0.2, 0.2]dampingVec2[5, 5]lookaheadVec2[0.2, 0]maxLookaheadVec2[2, 1]lookaheadSmoothingfloat3snapDistancefloat20
Scriptmain.aes
package demo

import "aego/input"

// Player walks with A/D or the arrows and jumps with Space, W or Up.
script Player {
    func Update(dt float) {
        m := node.Get<Motion2D>()
        x := 0.0
        if input.Down(input.KeyA) || input.Down(input.KeyLeft) {
            x -= 1
        }
        if input.Down(input.KeyD) || input.Down(input.KeyRight) {
            x += 1
        }
        m.Move(x)
        if input.KeyPressed(input.KeySpace) || input.KeyPressed(input.KeyW) || input.KeyPressed(input.KeyUp) {
            m.Jump()
        }
        if input.KeyReleased(input.KeySpace) || input.KeyReleased(input.KeyW) || input.KeyReleased(input.KeyUp) {
            m.JumpReleased()
        }
    }
}

// Lift moves a kinematic platform up and down.
script Lift {
    @export var Travel Vec2 = Vec2{0, -3}
    @export var Period float = 4

    var t float
    var start Vec2

    func Ready() {
        start = node.Transform2D().Position
    }

    func FixedUpdate(dt float) {
        t += dt
        phase := t / Period
        k := phase - float(int(phase))
        if k > 0.5 {
            k = 1 - k
        }
        k *= 2
        body := node.Get<Body2D>()
        body.MoveTo(Vec2{start.X + Travel.X*k, start.Y + Travel.Y*k}, 0)
    }
}