Unity Cinemachine: Over-The-Shoulder Aiming
A few articles ago, I talked about the state-driven camera and how I implemented the camera in my Unity project. Quick re-cap state-driven cameras work by assigning virtual cameras as children to the state-driven camera. Then you assign each animated state its camera, which will go live whenever the state is active. Today’s article will dive deeper into a specific animated state (aiming) and how I adjusted the camera to fit that state.
I have an animated state and a script that will activate the transition (bool) whenever the player holds a right click on the mouse.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Aiming : MonoBehaviour
{
private Animator _animator;
private bool _hasAnimatior;
void Start()
{
_hasAnimatior = TryGetComponent(out _animator);
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(1))
{
if (_hasAnimatior)
{
_animator.SetBool("Shooting", true);
}
}
else _animator.SetBool("Shooting", false);
}
}
Now for the state-driven camera, I have a virtual camera assigned to the animated state. This is a 3rd Person camera, with a different FOV and slightly closer to the player. Whenever aiming from any other state, it will smoothly transition to the over-the-shoulder view.
These are the different states and virtual cameras that the state-driven camera is in charge of. So whenever I press the right mouse button I now have the over-the-shoulder view.