How To Make Rotating Third Person Camera In Unity

Charles Brant

Coding Following Character-Orbitting Camera Control Script With Zoom In Unity

You know what’s really annoying? When you get to play a good game that has fixed camera in permanent place, or you only see your character from the top and a bit around him. This is exactly how to kill a good gameplay. If you play a game with beautiful world, then you obviously want to see more of it and from all angles. Coding a camera script is important task, do it poorly and the quality of other parts of your game won’t matter at all, but do it well and nothing will stop players from exploring the beautiful world of your game.

Not an exploration-type of game? Good camera is still essential for combat purposes.

This camera tutorial is part of a bigger guide about creating our first game in Unity and Blender. If you want to learn how to make models, animations, code C# scripts, use Blender and Unity as a whole, and make an 3D RPG, read my how to make a video game with Unity and Blender tutorial. Finishing it will give you solid Blender & Unity basics that you can use to make any kind of game :).

In previous article we learned Unity C# and programming 3D character movement script, which is a solid start. Not only we learned basics of C# programming language, but we also coded movement scripts, including rotation according to camera, jumping & running in all directions. Now that we have a full character movement script done, we can start building the outer environment of our game!

However before we do that, we still need a proper camera script that:

  • Follows the player.
  • Looks at the mouse.
  • Rotates according to mouse.
  • Has zoom and other settings.
  • Can be third or first person.

Honestly, it’s weird that such settings don’t exist in Unity’s Camera object by default – as everyone who wants to create 3D game will need a rotating/orbiting camera.


Rotating The Camera In Player Orbit

Create new script to object Main Camera (if u don’t have it, then recreate your scene, it should create on its own, if not, then just create new camera object). Call it CameraRotation, and copy-paste this:

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

public class CameraRotation : MonoBehaviour
{    
    float camPosX = 0f;
    float camPosY = 2f;
    float camPosZ = 1f;
    float camRotationX = 5;
    float camRotationY = 5;
    float camRotationZ = 5;
    float turnSpeed = 1;

    Vector3 offset = new Vector3(0,0,0);
    Vector3 abovePlayer = new Vector3(25,25,25);
    
    public Transform player;

    private void Start()
    {
        offset = new Vector3(player.position.x + camPosX, player.position.y + camPosY, player.position.z + camPosZ);
        transform.rotation = Quaternion.Euler(camRotationX, camRotationY, camRotationZ);
    }
    
    private void Update()
    {
        abovePlayer = new Vector3(player.position.x, player.position.y + 1, player.position.z);

        offset = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * Quaternion.AngleAxis(Input.GetAxis("Mouse Y") * turnSpeed, Vector3.right) * offset;

        transform.position = player.position + offset;
        transform.LookAt(abovePlayer);
    }
}

The settings are set for third person camera. If you want to make it first person, then you should play with float parameters (that are on the top). Okay, let me explain this script a bit.

First of all, to make it work you need to drag your character into the slot in Main Camera’s Inspector. That’s what public does in this line:

public Transform player;

It opens a slot in Inspector for an object of type Transform. Player is the name of variable, starting with lower letter. Transform is a class and using it in this manner means we can get and set its parameters.

Transform’s parameters are object’s rotation, position and scale. And thanks to this line we can make use of these parameters. Next line:

offset = new Vector3(player.position.x + camPosX, player.position.y + camPosY, player.position.z + camPosZ);

It creates offset between camera and player. Or actually not. It creates an Vector3 variable that will be later used as offset. Vector3 holds position data with our float parameters added to position of player. Next:

abovePlayer = new Vector3(player.position.x, player.position.y + 1, player.position.z);

We are creating Vector3 variable with player’s position. We also add 1 to Y axis. That’s because we will later force the camera to look in this direction. Player position alone is too low and you don’t see anything above character’s head by default. So I had to add 1. Next we are overwriting offset:

offset = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * Quaternion.AngleAxis(Input.GetAxis("Mouse Y") * turnSpeed, Vector3.right) * offset;

Quaternion.AngleAxis sets a rotation, in other words how many degrees we can rotate is specified by this method. First parameter is angle of type float and second parameter is Vector3 axis.

Our first parameter multiplies mouse position on X axis by turnSpeed and this is our angle. Second parameter is Vector3.up that means one point above.

You can see this “Quaternion.AngleAxis” is multiplied by another “Quaternion.AngleAxis”. That’s because one is for vertical angle, and another is for horizontal angle.

We are multiplying it by offset which is an existing Vector3. This is because we need to convert this variable to Vector3, but also because we need to add offset to our calculations, or the camera would be on the same position as player and would work in first person manner.


Hiding Camera Cursor

Add another script to our camera and name it Camera Cursor:

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

public class CameraCursor : MonoBehaviour
{
    void Update()
    {
        if(Input.GetKey(KeyCode.Escape))
        {
            Screen.lockCursor = false;
        }
        else
        {
            Screen.lockCursor = true;
        }
    }
}

This shows the cursor when we press Escape and hides otherwise (toggle). This is a must because otherwise the camera script is too confusing during gameplay.

And this is our camera script. Now we can see the world, its time to make it. So lets start from creating simple yet advanced drag and drop RPG inventory system. It uses simple solutions and doesn’t have much code, but it also works like proper inventory and has all the needed features like dropping items, rearranging and picking them up. So head to next chapter about creating drag and drop Unity inventory tutorial, inventories are really important part of RPG games, because with each picked up item our character gets a bit stronger :).

Comments