Make a character you can walk around
A Character Controller moves an object by sweeping its shape through the world. It walks up steps, stands on slopes, follows the floor downhill, rides moving platforms and pushes physics objects aside. Use it instead of a Rigidbody for anything that walks.
You need a floor with a collider. The one from Make things fall and collide is perfect.
Step 1: Make the player
- GameObject > 3D Object > Capsule, renamed
Player, at0, 1, 0. - Add Component > Physics > Character Controller. It needs no Rigidbody and no collider.
The capsule mesh's origin is at its middle, so the controller's default shape lines up with it. For a model whose origin is at its feet, set the controller's Center to half its height, such as 0, 0.9, 0.
Step 2: Write the movement script
Create a script called PlayerMovement:
using System;
using Tallow;
namespace MyFirstGame;
[Component]
class PlayerMovement : Script
{
public float walkSpeed = 4.5f;
public float runSpeed = 8;
public float jumpSpeed = 5.5f;
CharacterController mBody;
public override void Start()
{
mBody = GetComponent<CharacterController>();
}
public override void Update(TickInput input, float deltaTime)
{
// input.Move is WASD or the arrow keys: x is left/right, y is back/forward, each -1 to 1.
var move = Vector3(input.Move.x, 0, input.Move.y);
if (move.LengthSquared > 1)
move = move.Normalized;
if (move.LengthSquared > 0.01f)
Transform.Rotation = Quaternion.LookRotation(move, Vector3.Up); // face where we walk
let speed = input.Held(.Sprint) ? runSpeed : walkSpeed;
// You give the sideways velocity; gravity, landing and jumping are handled for you.
mBody.SimpleMove(move * speed, deltaTime, input.Pressed(.Jump) ? jumpSpeed : 0);
}
}
Add it to Player and press Play. W A S D (or the arrow keys) walk, Shift runs and Space jumps. A gamepad works too. Move, Jump and Sprint are input actions, and their keys can be changed in Window > Input Actions.
On 0.1: derive from
Component, read the keys withInput.GetKey(.W)and the others, and useTime.DeltaTime.
Step 3: Give it something to climb
Add a few cubes as steps and a ramp (a cube rotated about 25° on X), each with a Box Collider. Walk into them. In the Character Controller:
- Step Height is the tallest step it walks straight up.
- Max Slope is the steepest ramp it can stand on.
- Push Strength is how hard it shoves Rigidbodies. Walk into the box tower from the physics tutorial.
Step 4: Follow with the camera
Add a field for the camera and move it after everything else has moved, in LateUpdate:
public Camera view;
public Vector3 cameraOffset = .(0, 4, -7);
public override void LateUpdate()
{
if (!view) // no camera chosen yet
return;
view.Transform.Position = Transform.Position + cameraOffset;
view.Transform.Rotation = Quaternion.LookRotation(Transform.Position - view.Transform.Position, Vector3.Up);
}
Save, then drag the scene's Camera from the Hierarchy onto the View field in the Inspector. If your scene has no camera, add one with GameObject > Camera.
LateUpdate is only for this machine's view. It is never networked or replayed, which makes it the right place for cameras, effects and UI.
Step 5: Know whether you are on the ground
After each move the controller reports what happened. For example, to only allow running on the ground:
let speed = input.Held(.Sprint) && mBody.IsGrounded ? runSpeed : walkSpeed;
Other useful results are CanJump, AirTime, Velocity, GroundNormal and IsOnSteepGround. Teleport(position, velocity) moves the character somewhere else instantly.
Step 6: Where next
- Character controller covers box shapes, changing shape at run time and characters bumping into each other.
- Input shows how to rebind controls and add gamepad support.
- Networking: add a Network Identity to the player and this same script runs predicted in multiplayer.