Specific Game Mechanics

Third Person Follow Camera using CineMachine and Input System

You don’t have access to this lesson
Please register or sign in to access the course content.
Length: 30 minutes|Difficulty: Standard

Third person camera controls in Unity are difficult.

Thankfully Unity has a package called CineMachine which has a lot of plugins to assist you with this.

This guide[1] will cover how to use CineMachine and the new Input System[2] to create a camera that will follow the player.

You will need[3] to have a level created with a player object that can move forward and backwards before starting.

Here is an example movement script to attach to your player.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerLookCineMachineExample : MonoBehaviour
{

   private Vector2 _movement;
   private CharacterController _cc;
   public float PlayerSpeed = 5;

   // Start is called before the first frame update
   void Start()
   {
      _cc = GetComponent<CharacterController>();
   }

   // Update is called once per frame
   void Update()
   {
      MovePlayer();
   }

   void MovePlayer() {

      Vector3 movement = new Vector3(_movement.x, 0.0f, _movement.y);
      
      _cc.Move(movement * Time.deltaTime * PlayerSpeed);

      _cc.Move(Physics.gravity * Time.deltaTime);
   }


   
   void OnMove(InputValue iv) {
      _movement = iv.Get<Vector2>();
   }
}

Add the CineMachine package to your project[4]

You will also need the Input System package.

Add a Freelook Cinemachine camera to your scene.

The main camera has a CinemachineBrain added to it, you can ignore this.

As we are using the Input System we need to configure the camera to use it

Select the CM FreeLook1 camera and select AddComponent

Add a Cinemachine Input Provider

Attach the Look input action form the action map.

Attach the Player object to the Follow and Look At fields of CineMachineFreeLook

Now the camera will follow the player.

We now want to make it so that the player will turn toward the direction the camera is facing.

Here is the completed script to attach to the player.

The main addition is the RotatePlayer() method which uses math functions and Quaternion (lerp[5]) to smoothly turn the player to face their intended movement direction

Line 13 adds in the speed that the player will rotate towards the direction of movement.

Line 15 is the transform of the camera. This is used to set the direction to turn the player towards.

Line 35 is an addition which changes the Vector3 of the movement by multiplying the forward vector of the camera by the z axis of the movement and the right vector of the camera by the x axis of the movement.

Lines 42-46 contain the code to rotate the player in the direction of the movement.

Line 43 gets the angle to move the player.

Line 44 sets the angle to rotate around the y axis.

Line 45 actually rotates the player towards the movement direction.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerLookCineMachine : MonoBehaviour
{


   private Vector2 _movement;
   private CharacterController _cc;
   public float PlayerSpeed = 5;
   public float RotationSpeed = 75;

   private Transform _cameraTransform;

   // Start is called before the first frame update
   void Start()
   {
      _cc = GetComponent<CharacterController>();
      _cameraTransform = Camera.main.transform;
   }

   // Update is called once per frame
   void Update()
   {
      MovePlayer();
      RotatePlayer();
   }

   void MovePlayer() {

      Vector3 movement = new Vector3(_movement.x, 0.0f, _movement.y);
      
      movement = _cameraTransform.forward * movement.z + _cameraTransform.right * movement.x;
      
      _cc.Move(movement * Time.deltaTime * PlayerSpeed);

      _cc.Move(Physics.gravity * Time.deltaTime);
   }

   void RotatePlayer() {
      float playerTargetAngle = Mathf.Atan2(_movement.x, _movement.y) * Mathf.Rad2Deg + _cameraTransform.eulerAngles.y;
      Quaternion playerRotation = Quaternion.Euler(0.0f, playerTargetAngle, 0.0f);
      transform.rotation = Quaternion.Lerp(transform.rotation, playerRotation, Time.deltaTime * RotationSpeed);
   }
   
   void OnMove(InputValue iv) {
      _movement = iv.Get<Vector2>();
   }
}

Terms definitions
2. System. A set of interconnected parts designed to transform, store, transport or control[9] materials, energy and/or information.
3. need. An identified requirement of a person, group, or environment[7]. A need is identified from an issue[10] and sits within a context[11]. Technological practice[6] can be undertaken in an attempt to meet an identified need.
4. project. A series of tasks that need to be completed in order to reach a specific outcome[12]
5. lerp. Linear Interpolation, is a mathematical function[8] in Unity that returns a value between two others at a point on a linear scale
Terms definitions
1. guide. Length: 30 minutes|Difficulty: Standard Third person camera controls in Unity are difficult. Thankfully Unity has a package called CineMachine which has a lot of plugins to assist you with this. This guide will cover how to use CineMachine and the new Input System to create a camera that will follow the player. You will need […]
2. System. Length: 30 minutes|Difficulty: Standard Third person camera controls in Unity are difficult. Thankfully Unity has a package called CineMachine which has a lot of plugins to assist you with this. This guide will cover how to use CineMachine and the new Input System to create a camera that will follow the player. You will need […]
3. need. Length: 30 minutes|Difficulty: Standard Third person camera controls in Unity are difficult. Thankfully Unity has a package called CineMachine which has a lot of plugins to assist you with this. This guide will cover how to use CineMachine and the new Input System to create a camera that will follow the player. You will need […]
4. project. Length: 30 minutes|Difficulty: Standard Third person camera controls in Unity are difficult. Thankfully Unity has a package called CineMachine which has a lot of plugins to assist you with this. This guide will cover how to use CineMachine and the new Input System to create a camera that will follow the player. You will need […]
5. lerp. Length: 30 minutes|Difficulty: Standard Third person camera controls in Unity are difficult. Thankfully Unity has a package called CineMachine which has a lot of plugins to assist you with this. This guide will cover how to use CineMachine and the new Input System to create a camera that will follow the player. You will need […]