Skip to main content

Third Person Follow Camera using CineMachine and Input System

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 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>();
}
}

https://youtu.be/U3KX4wfKaDE

Add the CineMachine package to your project

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) 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>();
}
}