Making 1st/3rd Person Camera Orbit Player & Rotate In Unity

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

This tutorial is part of huge game making guide. In last article we created complete 3D character movement code and explained every line.

In this post we will code a 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.

In next part of guide I’ll go show how to create inventory system with drag and drop and picking up items.

Leave a Reply