read

Cinemachine is great, it's really powerful.Especially against living things In my humble opinion it is probably one of the best things Unity has added to the platform since I started using it 2 years ago. It is a fully fledged solution that allows you to create complex camera setups without writing a single line of code. (Though in this instance we will be writing code!)

This guide will assume a basic knowledge of setting up Cinemachine, I won't be going into that. I will be providing a guide on how I created a simple setup that you can see here:

Swishy

If you want an introduction to Cinemachine look no further than a talk given by one of it's creators, Adam Myhill of Unity Technologies. You can find it on the YouTubes here: https://youtu.be/r1SkOoJJRAA.

The idea is that when you hit a Trigger Volume (represented by the translucent yellow area) the camera will switch to another location so that the player is always kept in view. This is similar to classic survival horror games like Alone in the Dark and Resident Evil.

The scene contains two cameras, one of them follows a dolly and the other is fixed in place.

Cameras are represented by the Cinemachine Logo

Each camera is set up in a similar way, both are set up to "Look At" a follow target on the Player game object (The follow target being a simple empty child game object of the player game object) and the dolly cam is set up to follow the same target. Everything else is left at the defaults for this example.

An example of the Dolly camera

The scene also has a Trigger Volume present, this is a cube with a transparent yellow material on it so that we can see it. Attached to this object is a box collider with "Is Trigger" checked, and also a script called CameraTriggerVolume that I will explain next.

Simple Stretched out Cube, with a Trigger Collider and the below script

public class CameraTriggerVolume : MonoBehaviour
{
    public GameObject LinkedCamera;

    private CinemachineBrain _cameraBrain;

    private void Awake()
    {
        LinkedCamera.SetActive(false);
        _cameraBrain = Camera.main.GetComponent<CinemachineBrain>();
    }

    private void OnTriggerEnter()
    {
        SwapCameras();
    }

    private void SwapCameras()
    {
        var activeCam = _cameraBrain.ActiveVirtualCamera;

        LinkedCamera.SetActive(true);
        LinkedCamera = activeCam.VirtualCameraGameObject;

        activeCam.VirtualCameraGameObject.SetActive(false);
    }
} 

Let's go through what this code is doing...

The technical idea behind this script is to link a virtual camera to the trigger volume initially, this is the camera that you intend to switch to when the player enters the volume, we store this in the LinkedCamera property. We also need to get a reference to the CinemachineBrain stored on the MainCamera, we do this in the Awake method, while we're at it we might as well also set the linked cameras active state to false.

When the Trigger Volume is triggered, we call a private method called SwapCameras, in this method we first get a reference to the currently active camera, and activate the linked camera. We then replace the linked camera with the active camera, the reasoning for this is that when the volume is triggered again we will simply swap back to the previous camera. Allowing us to swap back and forth.

We then set the active camera to false, causing cinemachine to find the next virtual camera which is active, in this case the linked camera, making this the active camera.

If you try this out you will see that you can swap between two cameras, back and forth, by entering the Trigger Volume multiple times.

The interpolation between cameras is done by default, you can control the speed and easing of this via the Default Blend property in the Cinemachine Brain script.

Hopefully this will help you out in your projects!

This blog is made possible by generous donations on Patreon if you want to see more content like this then please consider donating, and be sure to follow us on Twitter for updates!

Blog Logo

Pixelbark


Published

Image
Back to Overview