Log inGet started
Package

third_person_follow.component

Newest version

This package holds more than this page lists. See all of its files in its world.

README.md

third_person_follow

Over-the-shoulder camera. The rig hangs off a shoulder pivot beside the follow target and sits back along the heading it looks down, so framing is authored as offsets in space. The offset is measured in the camera's own frame, so the subject holds the same spot on screen whichever way it turns.

The rig owns that heading. Binding a follow target aims it along the subject's facing, and look input turns it from there and the recentre turns it back behind the subject once the player stops looking around (below). A facing that becomes known or is declared anew after the bind aims it behind the subject again, once for each change: a Locomotion or LocomotionFull arriving on the subject or leaving it, a body standing up under it, as an avatar that composes after the camera bound does, or the component's forward or faceSign changing (CameraRig.facingSource names what the facing is read from). The heading holds while the body turns. That is what makes the camera steerable for a character who turns to face where it is walking: hold "forward" and the character walks the line the camera looks down, instead of chasing a heading that moves as it turns.

The facing a bind reads is the way the body visibly faces (CameraRig.visibleForward): a composed avatar body faces its -Z, turned by Locomotion or LocomotionFull as it walks, read through the rotation anchor that component turns, so the rig lands behind every avatar whatever file its body came from and after setCharacter swaps its body. A target with neither component answers its transform forward.

Public fields: follow, shoulderOffset (x across the frame, y up it, z along the heading), verticalArmLength, cameraSide (0 = left shoulder, 1 = right), cameraDistance, minDistance, maxDistance, aimYaw (the world heading the camera looks along, in radians about +Y from world +Z) and aimPitch (how far it tilts off level), damping (per-axis time constants in seconds of the camera's offset from the point it frames: they soften a change of framing, a stick or touch look, a zoom or a recentre; a mouse look turns the camera on the frame the mouse moves), horizontalLag, verticalLag, maxHorizontalLag, maxVerticalLag, jumpBand, leadTime, maxLead, leadSmoothing, recenterDelay, recenterTime, recenterTo, pullOutTime (see below), cameraRadius (radius of the pull-in sweep; 0 turns the pull-in off), cameraIgnore (entities whose colliders the pull-in passes through), occluderFade, occluderFadeWidth, bodyFade, bodyFadeDistance, fadeTime, fadeIgnore (fading thin occluders and the subject's body, see below), sensitivity, invertY (off by default: pushing the mouse forward looks UP; on pitches the other way), zoomSpeed, minPitch, maxPitch.

In edit mode, or while play is paused, writing any framing field re-poses the rig immediately with damping bypassed, so edit mode shows the framing play mode will produce. While play runs, a write steers the rig: its next tick takes the new framing through the damping and the pull-in, the way it takes look input, so a script that turns the camera by writing aimYaw swings it round over frames, coming in ahead of a wall as a look does.

follow falls back to the Camera.follow slot when it is empty, so a rig selected through Camera.behavior tracks whatever that camera already follows.

Controls: mouse to look (play mode requests pointer lock, so no button is held), scroll to adjust cameraDistance.

Following the subject: lag, lead and recentre

The camera frames a point that follows its subject's motion with lag, so a step, a stair, a landing, a start, a stop or a reversal is eased rather than copied:

  • horizontalLag / verticalLag (defaults 0.2 / 0.25 s) are the time constants of that point closing on the subject across the ground and along up; 0 carries that motion on the frame it happens.
  • maxHorizontalLag / maxVerticalLag (defaults 1.0 / 0.75 m) bound how far the subject gets from that point, however fast it goes, so the subject never leaves the middle of the frame by more.
  • jumpBand (default 1.5 m) holds the camera's height through a jump: while the subject is off the ground (its CharacterController, or MovementState, reads it so) it rises up to this far above the framed height with the camera standing still, so a jump that lands where it took off moves the view along up not at all. Past the band the camera rises with the subject, which stands no farther above the framed height than the band, so a higher jump or a launch keeps it in the picture. A landing higher or lower is eased to over verticalLag, and a fall below the framed height is followed as on the ground. 0 follows every jump.
  • leadTime (default 0.3 s), maxLead (1.5 m) and leadSmoothing (0.5 s): the aim runs ahead of the subject along its motion across the ground by its speed times leadTime, held to maxLead and eased as the subject starts, stops and turns, so the player sees where the subject is going. 0 aims at the subject.
  • recenterDelay (default 2 s) and recenterTime (1.25 s): in play, once look input has stopped for recenterDelay the heading turns back behind the subject over a time constant of recenterTime. recenterTo names what it turns behind: facing (the default), the way the subject visibly faces (its -Z, or the facing its Locomotion or LocomotionFull turns, CameraRig.visibleForward), or motion, the way it moves across the ground. While the subject moves the turn runs at the square of the share of its facing that points away from the camera (half the rate at 45 degrees off the view), so a subject running across the view or toward the camera keeps the heading where it is, rather than the camera and the subject turning each other round, and once it stops the turn waits recenterDelay again. A look the player makes while the subject stands holds the heading for as long as it stands: the wait starts once the subject moves off, and a subject that stops again with no look since turns behind after recenterDelay. A negative recenterDelay turns only on rig:recenter(), which turns the heading back now from any delay and ends a look's hold.
  • pullOutTime (default 0.4 s): once the way is clear the camera eases back out over this time constant. The pull-in itself never cuts (below).

Setting the two lags and leadTime to 0 gives the camera that carries its subject's motion on the frame it happens. rig:tracking() answers how the camera follows right now: lag (how far the subject stands from the framed point), lead, velocity (the subject's, as the rig measures it), recentering, idle (seconds since look input) and lookHeld (a look the player made holds the heading while the subject stands).

local rig = entity(camId).component.get("controller.third_person_follow")
rig.verticalLag = 0.4       -- ease stairs and jumps more
rig.recenterDelay = -1      -- keep the heading the player leaves it at
rig:recenter()              -- and turn it behind the subject on a button

The control scheme

inputMap is the scheme the rig answers to. It reads two controls from it, each on its own:

  • look — turns aimYaw and aimPitch.
  • zoom — steps cameraDistance.

A map declaring only one of the two gives the rig that one and keeps the other where it is; the rig logs which control the map left out, and names the map. A field naming no map leaves the rig answering nothing, and logs that too. Point inputMap at a world's own map to rebind looking and zooming without touching this component.

Repointing it on a live rig takes on the next frame. The map the rig was holding is stood down — its controls leave the live set and its touch buttons leave the screen — and the map named here is taken up in its place:

entity(rigId).component.get("third_person_follow").inputMap = asset.resolve("mygame.input.cinema", "inputMap")

Assigning the map the rig already holds changes nothing, so re-stating the scheme leaves the live set where it is. While gameplay is paused the rig holds no map at all — the editor's camera has the controls — and it takes whatever inputMap names when play resumes.

local cam = entity(id).component.get("Camera")
cam.behavior = asset.ref("@builtin::controller.third_person_follow", "component")
entity(id).component.get("controller.third_person_follow").cameraDistance = 3

The camera pulls in when a collider is in the way

Each frame the rig sweeps a sphere of cameraRadius from the shoulder point beside the subject itself (wherever the framed point lags) out to where its framing puts the camera, and stands the camera where the sphere first meets a collider. Every collider in the world stops that sweep, whether or not anything draws it: an invisible walk bound (a BoxCollider with no Model), a trigger volume and a solid wall all pull the camera in alike. The follow subject is the one entity the sweep passes by default.

The pull-in never cuts. Sweeps of that line ahead, from where the subject is moving to and along where the line is turning over the next 0.8 s, bring the camera in ahead of a wall end or a pillar about to come between it and the subject, at the speed that has it clear when the line gets there; only something no sweep could see coming (a wall that appears) brings it in on the frame it blocks the view, so no frame is drawn from behind geometry. The camera stands no nearer the sweep's start than 0.4 m while there is room round it: when the line has less room than that (the subject against a wall or a pillar the camera is behind), the camera turns round the subject about its up to the nearest turn that has the room, keeps looking the way its framing looks, and turns back onto the line once the line has the room again. A start touching a collider is swept with a thinner sphere, which slides along it, a start nearer it than the thinnest sphere has room for by a ray, and a start inside one holds the camera as far out as it stood.

So a camera that snaps in close to the player, or collapses onto it, while nothing visible stands behind the player is meeting a collider nobody can see. Two fields settle it:

  • cameraIgnore lists the entities whose colliders the camera sees through. Name the walk bounds and gameplay volumes there and the sweep passes them as it passes the subject, while walls the player can see still pull the camera in. Each entry is an entity (or its id), and it is the collider's own entity that is listed.
  • cameraRadius = 0 turns the pull-in off: the camera stands where its framing puts it, through every collider.
local rig = entity(camId).component.get("controller.third_person_follow")
rig.cameraIgnore = { entity.find("NorthBound"), entity.find("SouthBound") }
-- or drop the pull-in entirely
rig.cameraRadius = 0

Thin occluders and the subject's body fade instead of filling the view

In play the camera fades what would otherwise fill the view in place of the subject. A thin occluder (a pillar, a pole, a post, a railing, a doorframe's jamb, a tree trunk: a collider no wider than occluderFadeWidth across the line from the camera to the subject at its narrowest, and no deeper along it) does not pull the camera in: the sweeps pass it, and while it stands between the camera and the subject it fades to occluderFade, that share of its pixels left undrawn in a screen-space dither so the subject shows through. Walls, floors and every collider wider or deeper than that still pull the camera in, as above. The subject's own body fades as the camera comes within bodyFadeDistance of the aim point, reaching bodyFade at the nearest the camera stands (0.4 m), so a camera pulled in against a wall shows what lies past the subject rather than the back of its head. Each fade eases in and out over fadeTime, never in one frame.

A fade covers the entity and its descendants, in this engine's own views (each player's camera sets its own), and shadows stay whole. fadeIgnore lists entities the camera never fades: they pull it in whatever their size. occluderFade = 0 brings back the pull-in for every collider, and bodyFade = 0 keeps the body drawn whole. rig:fading() reports what the camera fades this frame, and renderer.drawStats().cameraFaded how many renderables carry a fade.

local rig = entity(camId).component.get("controller.third_person_follow")
rig.fadeIgnore = { entity.find("GateLever") }   -- a lever the player must always see
rig.occluderFade = 0                            -- or pull in for every collider

Attach through Camera.behavior

The Camera owns the behavior slot, so assigning the rig there is what lets the Camera tell it when follow changes. A rig added straight onto the entity with component.add still poses, but the Camera does not know about it and a follow assigned afterwards will not reach it.

Assigning the slot attaches the rig to the camera's entity, so it is tuned on that instance: component.get("third_person_follow") and set its fields. A component.add("third_person_follow", { ... }) after the assignment, in a scene build.luau or anywhere else, writes its values onto the instance the slot attached and returns it, so the Camera keeps the one rig it knows about. In a build.luau that add is the way to tune the rig: its values are saved with the scene and the loaded rig holds them.

Player cameras: leave follow unset

Entering play drops and respawns PlayerPrototype subtrees, so the body is recreated with a NEW entity id. A follow authored on the rig keeps pointing at the original, which is no longer live, and the camera freezes at its last pose instead of tracking the player.

For a player camera, set the target on the Camera and leave the rig's own follow empty:

entity(rigId).component.get("Camera").follow = body   -- PlayerPrototype rebinds this on every spawn

PlayerPrototype refreshes Camera.follow to the live body each time it clones, and the rig reads that slot whenever its own follow is empty. Set the rig's follow only for a camera that watches a fixed, non-respawning subject.