TExcelTAD
Home › Unity with C# › Cinemachine Cameras

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. CinemachineVirtualCamera and CinemachineFreeLook became CinemachineCamera with behaviour components, and the namespace is Unity.Cinemachine (was Cinemachine). Tutorials showing "Virtual Camera" and "Body/Aim" drop-downs are for Cinemachine 2.

How it works

  • The real Camera gets 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:

  1. Install Cinemachine from the Package Manager.
  2. 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.
  3. Set Tracking Target to the player. Add Cinemachine Follow for position and Cinemachine Rotation Composer for aim, or use a preset that does it.
  4. Add Cinemachine Deoccluder (named Collider in older versions) so walls don't block the view.

Switching shots from code

csharp
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:

csharp · try it yourself
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:

  1. A camera following a Rigidbody needs that Rigidbody's Interpolate turned on (or the Brain's Update Method set to Fixed Update).
  2. Move the target in one place only: physics bodies in FixedUpdate, CharacterControllers in Update.
  3. Don't also run your own follow script on the same camera.

Test yourself

In Cinemachine 3, which component replaces CinemachineVirtualCamera?

  1. CinemachineBrain
  2. CinemachineCamera
  3. CinemachineFreeLook
  4. VirtualCamera3

Which Cinemachine Camera drives the real camera?

  1. The newest one
  2. The active one with the highest priority
  3. All of them, averaged
  4. The one nearest the player

Why is Lerp(cam, target, 0.1f) every frame a poor smoothing method?

  1. Lerp is slow
  2. Its speed depends on the frame rate
  3. It never moves
  4. It only works in 2D