Cinemachine Cameras
Writing a good game camera is surprisingly hard: it should follow smoothly without lagging, frame the target, avoid walls, shake on impacts and cut or blend between shots. Cinemachine (com.unity.cinemachine) does all of that with components, so most Unity games never script the main camera directly.
Migrating: Cinemachine 3 (the version for Unity 6) renamed most types.
CinemachineVirtualCameraandCinemachineFreeLookbecameCinemachineCamerawith behaviour components, and the namespace isUnity.Cinemachine(wasCinemachine). Tutorials showing "Virtual Camera" and "Body/Aim" drop-downs are for Cinemachine 2.
How it works
- The real
Cameragets a Cinemachine Brain component. - You create any number of Cinemachine Cameras. They don't render; each one describes a shot: a target, how to follow it (a position control such as Follow, Orbital Follow, Third Person Follow) and how to aim (a rotation control such as Rotation Composer).
- The Brain drives the real camera from the live Cinemachine Camera: the active one with the highest Priority. When the live camera changes, the Brain blends (or cuts) according to its blend settings.
Setup:
- Install Cinemachine from the Package Manager.
- GameObject > Cinemachine > Cinemachine Camera (the same menu also has ready-made presets, such as a FreeLook camera). The Brain is added to the main camera automatically.
- Set Tracking Target to the player. Add Cinemachine Follow for position and Cinemachine Rotation Composer for aim, or use a preset that does it.
- Add Cinemachine Deoccluder (named Collider in older versions) so walls don't block the view.
Switching shots from code
using Unity.Cinemachine;
using UnityEngine;
public class CameraZone : MonoBehaviour // a trigger volume around a boss arena
{
[SerializeField] CinemachineCamera arenaCamera;
[SerializeField] CinemachineImpulseSource impulse;
void OnTriggerEnter(Collider other)
{
if (!other.CompareTag("Player")) return;
arenaCamera.Follow = other.transform; // the tracking target
arenaCamera.Prioritize(); // make it the top camera; the Brain blends to it
}
public void BossSlam() => impulse.GenerateImpulse(); // shake, received by cameras with an Impulse Listener
}Prioritize() puts a camera on top of its peers; you can also set priorities in the Inspector, or simply enable and disable Cinemachine Cameras, since a disabled one can't be live.
What "damping" means: a model
Cinemachine's position damping moves the camera a fraction of the remaining distance toward its goal each frame, scaled by time so it's frame-rate independent. A naive Lerp(cam, target, 0.1f) per frame is not: it follows faster at higher frame rates. The model compares both over half a second:
using System;
foreach (int fps in new[] { 30, 60, 144 })
{
double naive = Follow(fps, (cam, goal, dt) => cam + (goal - cam) * 0.1);
double damped = Follow(fps, (cam, goal, dt) => goal + (cam - goal) * Math.Exp(-dt / 0.15)); // time constant 0.15 s
Console.WriteLine(quot;{fps,3} FPS: naive lerp reaches {naive:F3}, exponential damping reaches {damped:F3}");
}
static double Follow(int fps, Func<double, double, double, double> step)
{
double cam = 0, goal = 10; // the target jumped 10 m away
double dt = 1.0 / fps;
int frames = fps / 2; // half a second
for (int i = 0; i < frames; i++) cam = step(cam, goal, dt);
return cam;
} 30 FPS: naive lerp reaches 7.941, exponential damping reaches 9.643
60 FPS: naive lerp reaches 9.576, exponential damping reaches 9.643
144 FPS: naive lerp reaches 9.995, exponential damping reaches 9.643
The exponential form gives the same camera on every machine. Use it (1 - Mathf.Exp(-speed * Time.deltaTime) as the lerp factor) whenever you write smoothing yourself.
Jitter checklist
If the camera or the player jitters:
- A camera following a Rigidbody needs that Rigidbody's Interpolate turned on (or the Brain's Update Method set to Fixed Update).
- Move the target in one place only: physics bodies in
FixedUpdate, CharacterControllers inUpdate. - Don't also run your own follow script on the same camera.
Test yourself
In Cinemachine 3, which component replaces CinemachineVirtualCamera?
CinemachineBrainCinemachineCameraCinemachineFreeLookVirtualCamera3
Which Cinemachine Camera drives the real camera?
- The newest one
- The active one with the highest priority
- All of them, averaged
- The one nearest the player
Why is Lerp(cam, target, 0.1f) every frame a poor smoothing method?
- Lerp is slow
- Its speed depends on the frame rate
- It never moves
- It only works in 2D