Aego Русский

Hitboxes

A Hitbox 2D damages the hurtboxes inside its area after the physics step. Policy decides how often one target is hit:

Policy Hits
onEnter every time the target enters
oncePerActivation once per target until Activate
periodic every Period seconds while they overlap

Team and Friendly Fire keep attacks off their own side; Team -1 takes the team of the nearest Health above, so a sword on a child node fights for its owner and never hurts it. Knockback is a velocity for a target with Motion 2D and an impulse for a dynamic body; it turns with the hitbox node, so a flipped character pushes the other way. Knockback From Center pushes away from the hitbox instead.

A sword swing is one call:

if input.Pressed("Attack") {
    Sword.Get<Hitbox2D>().Pulse()
}

Pulse() deals one round of damage in the next step, active or not; Activate() and Deactivate() switch a hitbox for the length of an attack. Hit(target, damage) fires for every target damaged.

Walk over the three spikes in the scene to compare the policies.

A hitbox selected in the Scene View: area handles and the knockback arrow

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
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]
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]
WallRight
Transform
position[19.5, 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]
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
modeenumplatformermaxSpeedfloat6jumpHeightfloat2.2stepHeightfloat0.25
Hurtbox 2D
areaRect[-0.3, -0.5, 0.6, 1]
Spike onEnter
Transform
position[3, 2.3]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint0tintColor#dc2626fforiginVec2[0.5, 0.5]sizeVec2[0.8, 0.4]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Hitbox 2D
areaRect[-0.4, -0.2, 0.8, 0.4]damageint1knockbackVec2[0, -5]teamint1policyenumonEnterperiodfloat0.5
Spike once
Transform
position[6, 2.3]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint0tintColor#dc2626fforiginVec2[0.5, 0.5]sizeVec2[0.8, 0.4]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Hitbox 2D
areaRect[-0.4, -0.2, 0.8, 0.4]damageint1knockbackVec2[0, -5]teamint1policyenumoncePerActivationperiodfloat0.5
Spike periodic
Transform
position[9, 2.3]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint0tintColor#dc2626fforiginVec2[0.5, 0.5]sizeVec2[0.8, 0.4]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Hitbox 2D
areaRect[-0.4, -0.2, 0.8, 0.4]damageint1knockbackVec2[0, -5]teamint1policyenumperiodicperiodfloat0.4
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("d6300000000000000000000000000003")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/debug"
    "aego/input"
)

// Player walks over three spikes and shows its hit points.
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()
        }
        hp := node.Get<Health>()
        p := node.WorldPosition()
        debug.Text(Vec2{p.X - 0.6, p.Y - 1}, "hp " + string(hp.Current), Color{1, 1, 1, 1})
    }
}

// Label writes Text above its node.
script Label {
    @export var Text string

    func Update(dt float) {
        p := node.WorldPosition()
        debug.Text(Vec2{p.X - 0.8, p.Y - 1}, Text, Color{0.85, 0.85, 0.85, 1})
    }
}