In this lesson, you will learn how to automate seamless camera transitions by using trigger volumes to adjust the priority of Cinemachine Virtual Cameras.
The Logic of Priorities
In Cinemachine, the "Cinemachine Brain" component on your Main Camera always chooses the Virtual Camera with the highest Priority value. When you want a camera to change automatically based on where the player is in the world, you don’t need to write complex state machines; instead, you can simply swap these priority values.
Setting Up the Trigger Volume
To achieve this, create a new GameObject in your scene with a Box Collider (or any shape) set to Is Trigger. This represents the "region" where you want a specific camera to take over.
Attach a script to this object that detects the player’s entry and exit. When the player enters, we increase the priority of our "Room Camera" to be higher than the "Default Camera." When they leave, we reset it.
using UnityEngine;
using Cinemachine;
public class CameraZone : MonoBehaviour
{
public CinemachineVirtualCamera zoneCamera;
public int highPriority = 10;
public int lowPriority = 0;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
zoneCamera.Priority = highPriority;
}
private void OnTriggerExit(Collider other)
{
if (other.CompareTag("Player"))
zoneCamera.Priority = lowPriority;
}
}
Why This Works
Because the Cinemachine Brain is constantly listening to the Priority property of all Virtual Cameras in the scene, it will immediately initiate a blend to the new camera the moment its priority surpasses the others. By using OnTriggerEnter and OnTriggerExit, you ensure that the camera transition is tied directly to the player's spatial context. This method is highly performant and allows you to create distinct "camera zones" throughout your map without coupling your camera logic directly to your character's movement script.
Key Takeaway: By dynamically updating Virtual Camera priorities via trigger volumes, you allow the Cinemachine Brain to handle transitions automatically based on the player’s current map region.