Aego Русский

Shake

In Try it, the crate drops every two seconds and its script calls camera.Shake(0.9) when it lands. Raise Max Angle or lower Exponent and Decay on the camera to feel the difference.

Shake is driven by trauma, a value from 0 to 1 per output. camera.Shake(0.4) adds trauma, which fades by Decay per second. The view moves by up to Max Offset and turns by up to Max Angle, scaled by trauma to the power Exponent, so small hits stay subtle. Without a Camera Shake 2D on the active camera the defaults are used.

The motion comes from deterministic noise, so a replay shakes the same way, and it never touches the node's transform or the game's random numbers.

Trauma adds up and fades in real time. With the default Decay of 1.5 a hit of 0.6 is gone in 0.4 seconds:

package demo

import (
    "aego/camera"
    "aego/log"
)

script Hits {
    var frames int

    func Ready() {
        camera.Shake(0.6)
        log.Info("hit, trauma " + string(camera.Trauma()))
    }

    func Update(dt float) {
        frames++
        if frames % 6 == 0 && frames <= 30 {
            log.Info("trauma " + string(camera.Trauma()))
        }
        if frames == 30 {
            camera.Shake(0.5)
            camera.Shake(0.8)
            log.Info("two hits, capped at " + string(camera.Trauma()))
        }
    }
}
On this page

Accessibility

Settings → Accessibility → Screen Shake scales every shake; 0 turns it off. Reduce Motion turns shake off and removes lookahead. Trauma still fades, so gameplay that reads camera.Trauma() is not affected.

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
Scene8 nodes
Camera
Transform
position[0, -1]rotation0scale[1, 1]
Camera Shake 2D
decayfloat1.5maxOffsetVec2[0.5, 0.5]maxAnglefloat0.05frequencyfloat25exponentfloat2
Virtual Camera 2D
priorityint0outputstring""zoomfloat0blendTimefloat0.5blendCurveCurve{ ease=inOutQuad }timeDomainenumunscaleduseBoundsboolfalseboundsRect[0, 0, 0, 0]snapToPixelsboolfalse
View
Transform
position[0, -1]rotation0scale[1, 1]
Camera 2D
zoomfloat40viewportRect[0, 0, 1, 1]maskint-1orderint0clearbooltrueclearColorColor#111827ffscaleenumfitVirtualvirtualVec2[960, 540]activebooltrue
Ground
Transform
position[0, 3]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint0tintColor#374151fforiginVec2[0.5, 0.5]sizeVec2[30, 1]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Rigid Body 2D
typeenumstaticmassModeenumautomassfloat0gravityScalefloat1linearDampingfloat0angularDampingfloat0fixedRotationboolfalsebulletboolfalseallowSleepboolfalseinterpolatebooltruemasklayer mask-1
Box Collider 2D
offsetVec2[0, 0]isTriggerboolfalsefrictionfloat0.6restitutionfloat0.1densityfloat1sizeVec2[30, 1]
Post1
Transform
position[-7, 1.5]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint0tintColor#3b82f6fforiginVec2[0.5, 0.5]sizeVec2[1, 2]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Post2
Transform
position[-4.5, 1.5]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint0tintColor#22c55efforiginVec2[0.5, 0.5]sizeVec2[1, 2]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Post3
Transform
position[4.5, 1.5]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint0tintColor#22c55efforiginVec2[0.5, 0.5]sizeVec2[1, 2]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Post4
Transform
position[7, 1.5]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint0tintColor#3b82f6fforiginVec2[0.5, 0.5]sizeVec2[1, 2]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Crate
Transform
position[0, -6]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint0tintColor#f59e0bfforiginVec2[0.5, 0.5]sizeVec2[2, 2]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Rigid Body 2D
typeenumdynamicmassModeenumautomassfloat0gravityScalefloat1linearDampingfloat0angularDampingfloat0fixedRotationbooltruebulletboolfalseallowSleepboolfalseinterpolatebooltruemasklayer mask-1
Box Collider 2D
offsetVec2[0, 0]isTriggerboolfalsefrictionfloat0.6restitutionfloat0.1densityfloat1sizeVec2[2, 2]
Scriptmain.aes
package demo

import (
    "aego/camera"
    "aego/physics"
)

// Drop lifts the crate every few seconds; each landing shakes the camera.
script Drop {
    @export var Every float = 2
    @export(min=0, max=1)
    var Hit float = 0.9

    var t float
    var start Vec2

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

    func Update(dt float) {
        t += dt
        if t >= Every {
            t = 0
            body := node.Get<Body2D>()
            body.Teleport(start, 0)
            body.SetVelocity(Vec2{0, 0})
        }
    }

    func OnCollisionEnter(other NodeRef, c physics.Contact) {
        camera.Shake(Hit)
    }
}