EngineIntermediate

Characters

Move CharacterVirtual objects, inspect ground state, crouch and jump, and use the model and clips a new character ships with.

Character movement

Characters use Jolt CharacterVirtual. Set horizontal velocity directly and preserve the current Y component so gravity and jumping continue naturally.

local velocity = gameObject.getCharacterVelocity()
gameObject.setCharacterVelocity(0, velocity.y, 5)

The engine does not move a character on its own: its velocity is whatever your script last set, plus gravity. The inspector's Walk Speed and Run Speed are for your script to read, with gameObject.getWalkSpeed() and gameObject.getRunSpeed() (both in metres per second, and nil on anything but a character). The engine never applies them itself, so a controller that reads them lets you tune each character's speed in the inspector without editing the script. The starter controller reads Run Speed. Jump Speed is the one movement field the engine uses; see below.

A character is not a rigid body. addForce, addImpulse, addTorque, and setLinearVelocity do nothing on it. To knock a character back, add to its velocity with setCharacterVelocity. To turn it, use setRotation or rotate.

A character does report what it touches. Once its script calls gameObject.registerForCollisions(), onCollisionEnter and onCollisionExit fire for the floor it lands on, the walls it walks into, and any body or other character it touches, and the other object's script gets the same events. A pickup a player walks into can therefore detect it from its own onCollisionEnter. See Physics, collisions, and triggers.

Balls and other light bodies

A character treats a dynamic body lighter than a tenth of its own mass (7 kg for the default 70 kg character) as light: a football, a can, a small box. Walking into one still kicks it, as hard as the character's Max Strength allows. But a light body cannot push the character, cannot carry or lift it, and is never taken for a stair step. A ball rolled at a standing player bounces off it, and a character chasing a rolling ball stays on the ground.

The line is relative to the character's mass. Heavier dynamic bodies, kinematic platforms and static ground behave as before: a character can stand on them, ride them and step onto them. Trigger volumes are never light bodies.

Ball games. A chasing or dribbling script can keep the vertical velocity like any other controller:

local SPEED = 6

function update(deltaTime)
  local ball = gameObject.getObjectByName("Ball")
  if not ball then return end
  local me = gameObject.getPosition()
  local target = ball.getPosition()
  local dx, dz = target.x - me.x, target.z - me.z
  local len = math.max(math.sqrt(dx * dx + dz * dz), 0.001)
  local velocity = gameObject.getCharacterVelocity()
  gameObject.setCharacterVelocity(dx / len * SPEED, velocity.y, dz / len * SPEED)
end

Walk-stairs never steps onto a light body, however small it is. A character can still land on one by jumping or falling onto it, but it gets no lift from it.

Ground state

  • isOnGround()
  • getGroundNormal() and getGroundVelocity()
  • getGroundObjectId(), getGroundObject(), and getGroundInfo()
  • getSlopeAngle() and isSlopeTooSteep()
  • getUp(), the character's up direction

isOnGround() is true only when the character stands on something it can stand on. On a slope steeper than the inspector's Max Slope Angle (45° by default) it is false even though the character is touching the ground, and isSlopeTooSteep() is true. getSlopeAngle() is in degrees and returns 0 when the character touches nothing, so ask isOnGround() rather than reading 0 as a flat floor. getGroundNormal() is nil in the air. getGroundInfo() returns all of it in one table: objectId, objectName, slopeAngle, isTooSteep, velocity, and normal.

Riding a moving platform. A kinematic platform moved with moveKinematic lifts and lowers a character standing on it, and getGroundVelocity() (and getGroundInfo().velocity) then returns the platform's velocity. Sideways travel is not carried for you, so a control script that should ride adds it while the character is on the ground:

local velocity = gameObject.getCharacterVelocity()
local ground = { x = 0, z = 0 }
if gameObject.isOnGround() then
  ground = gameObject.getGroundVelocity()
end
gameObject.setCharacterVelocity(moveX + ground.x, velocity.y, moveZ + ground.z)

This also carries a character round a turntable or spinning disc. Its facing does not turn with the platform on its own; to turn it, add the ground's spin while on the ground. getGroundAngularVelocity() is in radians per second about world axes, and zero in the air:

if gameObject.isOnGround() then
  gameObject.rotate(0, gameObject.getGroundAngularVelocity().y * deltaTime, 0)
end

A script that keeps its own yaw adds getGroundAngularVelocity().y * deltaTime to it instead. See Kinematic bodies and moving platforms.

Jumping and crouching

jump() uses the character’s configured jump speed. It only works while isOnGround() is true and the character is not already rising, so calling it every frame the button is held gives one jump, not a rocket. It adds Jump Speed (6 by default) to the character's upward velocity rather than replacing it. In water it swims upward instead — see below.

setCrouched(true) swaps to the crouching capsule set by Crouch Radius and Crouch Half Height (1 m tall by default) and returns whether the capsule could change safely; isCrouched() reports the current state. Standing back up fails under a ceiling too low for the standing capsule: setCrouched(false) returns false and the character stays crouched. Call it every frame with the button's state, as below, and the character stands as soon as there is room.

To give the player a crouch control, add an action button with the ID crouch and pass input.crouch straight to setCrouched(): it is true while the button is held, so releasing it stands the character back up. In a multiplayer room every player sees the character crouch. See Player input and touch controls.

In water

Inside a water object a character is buoyed up, slowed, and carried by the water's current, using the same Buoyancy, Linear drag, and Current velocity settings that act on rigid bodies. isInWater() is true while any of it is below the surface, and getWaterSubmersion() returns how much of its height is under, from 0 to 1. jump() swims upward in water — it raises the upward speed to half of Jump Speed — and works whether or not the character is touching the bottom. See Physics, collisions, and triggers for the water object's settings.

Walking on a planet

A character can have its own up and its own gravity, so it can walk all the way round a sphere. Grounding, Max Slope Angle, stick-to-floor, stair steps, jump() and automatic animation are all measured against the character's up, and its model is turned so it stands upright on it. Planet gravity is single player only.

The usual way is radial gravity, set once from the scene script (see Scene scripts): every character then falls toward the planet and stands up away from it on its own. gameObject.setCharacterUp(x, y, z) takes the up over yourself, and setCharacterUp(nil) hands it back. gameObject.setCharacterGravity(x, y, z) gives one character its own gravity (still scaled by its gravity factor), and nil removes it; getCharacterUp() and getCharacterGravity() read them.

A control script that builds a yaw rotation and keeps velocity.y assumes world Y is up. On a planet, keep the same maths but use the character's own frame: setCharacterHeading(yaw) instead of setRotation, and getCharacterLocalVelocity()/setCharacterLocalVelocity(x, y, z) instead of getCharacterVelocity/setCharacterVelocity. Local y is along the up; x and z are across it. On flat ground these are exactly the old calls, so one script works in both places.

local yaw = 0
function init()
  yaw = gameObject.getCharacterHeading()
end

function update(deltaTime)
  -- turn yaw and work out moveX / moveZ from it as before, then:
  gameObject.setCharacterHeading(yaw)
  local velocity = gameObject.getCharacterLocalVelocity()
  gameObject.setCharacterLocalVelocity(moveX * speed, velocity.y, moveZ * speed)
  if input.jump and gameObject.isOnGround() then gameObject.jump() end
end

The frame is carried round with the character as its up turns, so its heading never jumps, even over the poles. Built-in first- and third-person cameras keep the character's up automatically; call setCameraUpMode("world") on the camera to keep world up instead. setCharacterUp and setCharacterGravity raise an error in a multiplayer room. Stop puts every character back on world up and the scene's gravity.

Played by a bot

A character, wheeled vehicle or tracked vehicle can be played by a bot instead of the player: an NPC that walks, wanders or follows your own logic, using the same control script a player's character uses. Set Played by in the inspector, under the Lua Script field:

Played byWhat drives it
Player (the default)The player's input.
Bot: WanderWalks in a direction for a while, then picks another.
Bot: RoamWalks to random points on the scene's navigation mesh, 4 to 14 m around where the game last placed it, and walks home when it gets stuck.
Bot: ScriptThe scene script's onBotThink(bot, dt); see below.
Bot: IdleNo input at all: it stands still.

A bot does not move the object itself. It supplies the input the object's control script reads, exactly as a player's keys or stick would: moveAxisX/moveAxisY, the buttons, and also input.aimYaw and input.aimPitch, the direction it is facing in world-space radians (a player's input carries no aim outside a multiplayer room). So the object needs a control script, and the inspector warns when it has none. The player's input never reaches a bot's object, and a bot's input never reaches the player's. Wander moves along moveAxisX/moveAxisY in world space; Roam always aims input.aimYaw where it is going. It starts by pushing moveAxisY forward, which suits a script that turns the body toward the aim. If the character doesn't go where it meant (say, a script under a fixed camera that moves along world axes and never reads the aim), it switches to world-direction axes within a couple of metres and keeps whichever works.

Bot settings. Under Played by, Wander and Roam have settings that tune how they play. Leave a box blank to keep its default. Script and Idle have no settings: a script bot's input is whatever onBotThink returns.

SettingWhat it doesDefault
SpeedHow far the bot pushes the move stick, from 0.1 to 1. A control script that scales speed by the stick moves the bot slower at a lower setting.0.8
JumpsUnticked, the bot never presses jump, including the jump Roam uses to get unstuck.On
FiresUnticked, the bot never presses fire.On
Roam min / Roam maxRoam only: how far from home each target is, in metres, from 1 to 100. A range given the wrong way round is turned round.4 / 14

A guard that patrols a small courtyard without shooting, for example, is Roam with Speed 0.4, Fires off and Roam max 6. Through MCP these are the object's botSettings field: {"speed": 0.4, "fires": false, "roamMax": 6}.

A script can change who plays an object during a run with setBotBrain(name) and setBotBrain(nil), and read it with getBotBrain(); see gameObject API. setBotBrain(name, settings) also replaces the bot's settings for the rest of the run, for example setBotBrain("roam", { speed = 1, roamMax = 30 }) when a guard starts a chase. Without a table, the object keeps the settings it already has. Stop puts back the authored setting and its settings.

Script bots. With Bot: Script the scene script decides. Define onBotThink(bot, dt); it is called for each such object about 30 times a second of game time, with bot = {id = "bot:" .. objectId, objectId = ..., isBot = true}, and returns the input table described in Lua scripting overview, or nil for no input. authorityActions are ignored outside a test room's seated bots, and reported once. With no scene script, or no onBotThink, the bot stands idle and a warning says so.

-- Scene script: every Script bot walks toward the player
function onBotThink(bot, dt)
  local me = scene.getObject(bot.objectId)
  local player = scene.getObjectByName("Player")
  if not me or not player then return nil end
  local a, b = me.getPosition(), player.getPosition()
  local dx, dz = b.x - a.x, b.z - a.z
  local len = math.sqrt(dx * dx + dz * dz)
  if len < 2 then return nil end
  return { moveAxisX = dx / len, moveAxisY = dz / len, aimYaw = math.atan(dx, dz) }
end

Bots run in the editor, Preview and published single-player games, and in physics_run_simulation, where a bot's object is played by its brain and never by the run's input_sequence. In a multiplayer room they run on the room server: see "Objects played by a bot" in Build and test a multiplayer game.

Where a character's origin sits

A character's position is at its feet, not its centre. Its bounds run from y = 0 up to its full height, so a 2m character standing on a floor at y = 0 has position.y = 0, and its eye is at roughly position.y + 1.65.

This differs from primitives — a capsule with the same collider dimensions is centred, so its bounds straddle its origin. See Coordinates and transforms for the full per-type table. It is the single most common source of a first-person camera that renders from hip height.

What a new character looks like

A character needs no mesh asset of its own. One with no meshAssetId renders a bundled model: a humanoid roughly 1.8m tall, facing local +Z, with its origin at its feet — the same conventions above. Choosing a mesh asset in the inspector replaces it; the empty option puts it back.

The model is not fitted to the collider. A new character's capsule is 1.5m tall (Radius 0.25, Half Height 0.5), so the model's head rises about 0.3m above it. Raycasts, sphere queries, and head-height clearance all use the capsule, not the model. To match the model, set Half Height to 0.65, which makes the capsule 1.8m.

That model carries thirty animation clips, covering locomotion in all eight directions, turns on the spot, the air, crouching and a sneaking walk, a roll, a vault and a climb, a small combat set with a reload, a look-around and an emote. gameObject.enableAutoAnimation({}) with no arguments drives the locomotion ones correctly, because every clip name it looks for is a name the bundled model already uses. Animation playback API lists them all and documents the state machine.

The character's Current Animation dropdown in the inspector lists whichever clips the loaded model actually has, which is the quickest way to see them without running the scene.

A character's clips play in place: the horizontal movement of the root bone is removed so the model stays on its capsule, while vertical movement is kept, so a death or crouch clip lowers the body to the floor. Move the character across the ground from its script. This also applies to clips added with Add Animation. See "Root motion" in Animation playback API, which also covers attaching a weapon or a hat to one of the model's bones.

A character created through an MCP client or the AI assistant gets the same New Character settings as one added from the editor's menu (the 1.5m capsule, Walk Speed 4, Run Speed 8, Jump Speed 6, mass 70, and the rest) for every field the request leaves out.

That includes Show Collision Shape (showCollisionShape), which is on for a new character either way. In the editor it draws a green wireframe of the capsule round the model, so you can see where the collider sits against it; turn it off to hide the wireframe. Preview and published games never draw it.

Cameras and input

Following a character with a camera — controlMode, targetCharacterId, eye height, the follow boom, pitch limits, and hiding the character's own body from a first-person view — is covered in Cameras and follow modes.

Turning the body toward the view. In a multiplayer room a control script can read the player's aim as input.aimYaw and input.aimPitch, in world-space radians, and turn the character to face it with gameObject.setRotation(0, math.sin(yaw / 2), 0, math.cos(yaw / 2)). Pair that with a First Person (Absolute Aim) camera (first_person_absolute). A plain first_person camera adds look to the body's rotation, so turning the body from look turns the view twice.

What actually produces movement and look on each device, how the touch sticks behave, and how authored action buttons reach Lua are covered in Player input and touch controls.