OrbitCamera
A third-person chase camera: it sits behind and above a target entity, looking at a point above the target's feet, and swings round that point on a sphere. A look vector (e.g. a right stick) orbits it (x) and tilts it (y); recenter() swings it back behind the target. It never drifts behind a moving target on its own, so camera-relative movement (see BGE.Math.cameraRelativeMove()) runs straight instead of curving. lockOn() turns it to keep a second entity (e.g. an enemy) in view beyond the target.
It's a BGE.FollowCamera, so a scene can swap it for another rig (e.g. a BGE.ChaseCamera). Give it a BGE.CameraBoom to pull it in front of walls between it and the target. Call update() after everything has moved, e.g. in the scene's onPostUpdate():
m.orbit = new BGE.OrbitCamera(camera, player, { boom: new BGE.CameraBoom(m.game, {collidableFlags: CAMERA_BLOCKER}) }) m.orbit.update(dt, m.game.controls.getRawAxis("look"))
Properties
camera(BGE.Camera3d)target(BGE.GameEntity)heading(float) — Ground heading the camera looks along (heading 0 faces -z)distance(float) — Horizontal distance behind the target at rest, in world unitsheight(float) — Height of the camera above the target's position at restlookHeight(float) — Height above the target's position of the point the camera looks atminHeight(float) — The camera never goes lower than this above the target's positionorbitSpeed(float) — Radians/sec at full lookrecenterSpeed(float) — Radians/sec while recentering (tilt eases back at a quarter of this)tilt(float) — Tilt added to the resting angle, in radians; positive looks further down on the targettiltSpeed(float) — Radians/sec at full lookminTilt(float)maxTilt(float)boom(BGE.CameraBoom) — Optional: pulls the camera in front of blockerslockTurnSpeed(float) — Radians/sec while turning to keep a locked-on entity in viewlockTilt(float) — Tilt while locked on, e.g. positive to look down on the ground between the target and thelockTarget(BGE.GameEntity) — The entity the camera keeps in view beyond the target, or invalid; see lockOn()lockDeadZone(float) — Locked on, the camera doesn't turn while the entity is in the middlelockDeadZoneof thelockEase(float) — Locked on, an exponential ease: each frame the camera turns 1 - e^(-lockEase * dt) of therecentering(boolean)noLook(dynamic) — follow()'s look when the input has none; update() only reads itlockRefocusing(boolean) — Turning back toward a lockOn() entity that left the dead zone
Constructor
new OrbitCamera(
camera: BGE.Camera3d,
target: BGE.GameEntity,
options?: BGE.OrbitCameraOptions,
): OrbitCameraPlaces the camera straight away, behind the target unless options.heading says otherwise, and levels its roll (e.g. left over from a BGE.ChaseCamera).
Parameters
camera(BGE.Camera3d) — the camera to movetarget(BGE.GameEntity) — the entity to follow; its rotation.y is "behind"options(BGE.OrbitCameraOptions, optional, default: "{}") — overrides for the public fields
Instance Methods
snapBehind(heading: float): void
Puts the camera straight behind the target, looking along heading, level and with no easing, e.g. after a respawn or a cut. Cancels a recenter(); a lockOn() carries on.
Parameters
heading(float) — radians; 0 faces -z, increasing turns clockwise seen from above
Returns
void
follow(dt: float, input: BGE.FollowCameraInput): void
update() with a BGE.FollowCameraInput: reads look (none if left out).
Parameters
dt(float) — seconds since last frameinput(BGE.FollowCameraInput)
Returns
void
recenter(): void
Starts swinging the camera back behind the target, with the tilt back to rest. Any look input cancels it.
Returns
void
lockOn(entity: BGE.GameEntity): void
Turns the camera to look past the target at entity and keep it there, tilted to lockTilt. Look input is ignored until releaseLock(), or until entity is invalidated.
Parameters
entity(BGE.GameEntity) — what to keep in view, e.g. a locked-on enemy
Returns
void
releaseLock(): void
Stops following a lockOn() entity; the camera stays where it is.
Returns
void
isLocked(): boolean
Whether the camera is keeping a lockOn() entity in view.
Returns
boolean
isRecentering(): boolean
Whether a recenter() is still under way.
Returns
boolean
update(dt: float, look: BGE.Math.Vector): void
Orbits/tilts by the look vector (or turns toward a lockOn() entity), then moves the camera.
Parameters
dt(float) — seconds since last framelook(BGE.Math.Vector) — x orbits (positive turns the camera clockwise seen from above), y tilts (positive looks up); each -1..1. Ignored while locked on.
Returns
void
lockTargetScreenOffset(): float
How far off the middle of the screen the lockOn() entity is, across: 0 in the middle, 1 at the left or right edge, more when off screen. Measured on the ground from where the camera is, so it ignores tilt.
Returns
float
turnTowardLockTarget(dt: float): void
Parameters
dt(float)
Returns
void
getLookAt(): BGE.Math.Vector
The point the camera looks at: lookHeight above the target.
Returns
BGE.Math.Vector
apply(dt: float): void
Parameters
dt(float)
Returns
void