Aego Русский

Camera Follow 2D

Moves its virtual camera after Target, with a deadzone, damping and lookahead.

component camera.CameraFollow2D
On this page

Usage

Needs Virtual Camera 2D on the same node.

Try it

Snap after a teleport

The target teleports eight world units; very slow damping makes the camera's response easy to see.

Change Snap Distance from 20 to 1: the camera snaps to the teleported target. Reset restores the original slow follow.

Starts the scene; open the example parameters to experiment.
Open in AegoDownload example project
Scene9 nodes
Ramp
Transform
position[0, 2]rotation0.12scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint0tintColor#4b5563fforiginVec2[0.5, 0.5]sizeVec2[60, 0.6]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Rigid Body 2D
typeenumstaticmassModeenumautomassfloat0gravityScalefloat1linearDampingfloat0angularDampingfloat0fixedRotationboolfalsebulletboolfalseallowSleepbooltrueinterpolateboolfalselayerlayer0masklayer mask-1
Box Collider 2D
offsetVec2[0, 0]isTriggerboolfalsefrictionfloat0.2restitutionfloat0densityfloat1sizeVec2[60, 0.6]
Wall
Transform
position[29, 2]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint0tintColor#4b5563fforiginVec2[0.5, 0.5]sizeVec2[0.6, 6]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Rigid Body 2D
typeenumstaticmassModeenumautomassfloat0gravityScalefloat1linearDampingfloat0angularDampingfloat0fixedRotationboolfalsebulletboolfalseallowSleepbooltrueinterpolateboolfalselayerlayer0masklayer mask-1
Box Collider 2D
offsetVec2[0, 0]isTriggerboolfalsefrictionfloat0.6restitutionfloat0densityfloat1sizeVec2[0.6, 6]
Post1
Transform
position[-20, -1.7]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint0tintColor#1f2937fforiginVec2[0.5, 0.5]sizeVec2[0.3, 2]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Post2
Transform
position[-10, -0.5]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint0tintColor#1f2937fforiginVec2[0.5, 0.5]sizeVec2[0.3, 2]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Post3
Transform
position[0, 0.7]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint0tintColor#1f2937fforiginVec2[0.5, 0.5]sizeVec2[0.3, 2]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Post4
Transform
position[10, 1.9]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint0tintColor#1f2937fforiginVec2[0.5, 0.5]sizeVec2[0.3, 2]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Post5
Transform
position[20, 3.1]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint0tintColor#1f2937fforiginVec2[0.5, 0.5]sizeVec2[0.3, 2]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Ball
Transform
position[-26, -2]rotation0scale[1, 1]
Sprite Renderer
spriteTextureasset("ae900000000000000000000000000010")layerint0orderint1tintColor#f59e0bfforiginVec2[0.5, 0.5]sizeVec2[1, 1]flipXboolfalseflipYboolfalseblendenumpremultipliedfilterenumnearest
Rigid Body 2D
typeenumdynamicmassModeenumautomassfloat0gravityScalefloat0linearDampingfloat0angularDampingfloat0fixedRotationboolfalsebulletboolfalseallowSleepbooltrueinterpolateboolfalselayerlayer0masklayer mask-1
Circle Collider 2D
offsetVec2[0, 0]isTriggerboolfalsefrictionfloat0.6restitutionfloat0.1densityfloat1radiusfloat0.5
Camera
Transform
position[-26, 0]rotation0scale[1, 1]
Camera 2D
zoomfloat40viewportRect[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("d5000000000000000000000000000008")deadzoneRect[0.4, 0.4, 0.2, 0.2]dampingVec2[0.0001, 0.0001]lookaheadVec2[0, 0]maxLookaheadVec2[3, 1]lookaheadSmoothingfloat3snapDistancefloat20
Scriptmain.aes
package demo

import "aego/physics"

// Let physics create the body and the camera observe the initial pose, even
// when several fixed steps happen in one rendered frame.
script Launch {
    var physicsReady bool
    var frames int
    func FixedUpdate(dt float) {
        physicsReady = true
    }
    func Update(dt float) {
        if physicsReady {
            frames++
            if frames == 2 {
                body := node.Get<Body2D>()
                body.Teleport(Vec2{-18, -2}, 0)
                body.SetVelocity(Vec2{0, 0})
            }
        }
    }
}

Fields

NameTypeDefaultDescription
TargetNodeRefnone

Node to follow; its interpolated position is used.

DeadzoneRect(0.4, 0.4, 0.2, 0.2)

Area of the view, in 0..1 of its width and height, where the target moves without moving the camera.

DampingVec2(6, 6)

How fast the camera catches up, per second on each axis. 0 follows rigidly.

LookaheadVec2(0.25, 0)

Seconds of target velocity the camera looks ahead on each axis.

Max LookaheadVec2(3, 1)

Largest lookahead in world units on each axis. 0 is unlimited.

Lookahead Smoothingfloat3

How fast the lookahead changes, per second. 0 changes at once.

≥ 0
Snap Distancefloat20

A target jump longer than this, in world units, moves the camera at once. 0 never snaps.

≥ 0

Methods

Snap method

func (CameraFollow2D) Snap()

Moves the camera onto the target at once, for example after a teleport.