fbpx
Read Time:4 Minute, 48 Second

In video games, usually, allowing the player to move the camera is one of the most important things.
Along with allowing player movement
Try to think about games where you have a character controlled by the player with no camera movement.
There are a few, surely, but they are not many.

So the first thing we (or I) want to allow the user to do is move around the map.
Oh and also look around, of course.

Looking Around

In this project, I want the camera to move with the player when he is moving around.
In order for the camera to move with the player, it is simply a matter of making the camera a child of the player object and placing it where we want relative to the player.

We also want to allow the user camera movement and look around.
There are two ways we can approach it.

As The Player Object’s child

Remember when we set the camera as a player object’s child? Well, we can use it.
We can use it and make the camera rotate with the player.

Using this approach, we will simply write our script and and attach it to the player object.
This way, the camera will not only move with the player but also rotate with it.

Rotate On Its Own

Though the first approach is good, sometimes we want the camera movement to be independent of that of the player.
And so, the fact that the camera is a child of the player object does not bind us to use only as such.

Knowing that, we can assign the script to the camera object instead of assigning it to the player object.
That way, the camera will rotate but the play will not.

This is also the method we will implement here since we are trying to create a FPS game.

Implementing Camera Movement

Now that we know how we want the camera to move, let us implement it.

First, we will create a new script called “MouseLook” and open it for editing.

This is the code we want to use (explanation after the code).

using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;

public class MouseLook : MonoBehaviour
{

    public float mouseSensitivity = 100f;
    public Transform playerBody;
    public Transform rocketLauncher;

    private float xRotationCamera = 0f;
    private float xRotationRocketLauncher = 90f;

    // Start is called before the first frame update
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }

    // Update is called once per frame
    void Update()
    {
        float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

        xRotationCamera -= mouseY;
        xRotationCamera = Mathf.Clamp(xRotationCamera, -90f, 90f);

        xRotationRocketLauncher -= mouseY;
        xRotationRocketLauncher = Mathf.Clamp(xRotationRocketLauncher, 0f, 180f);

        transform.localRotation = Quaternion.Euler(xRotationCamera, 0f, 0f);
        playerBody.Rotate(Vector3.up * mouseX);
        rocketLauncher.localRotation = Quaternion.Euler(xRotationRocketLauncher, 0f, 0f);
    }
}

public float mouseSensitivity = 100f – As the name of the field implies, this will help us to change the mouse sensitivity and we set it to 100f as default.

public Transform playerBody which we will be using to make the player rotate with the camera along the y axis.

This guide is the start of the project in which we start small and simple.
The best way to do that, we are trying to mimic Overwatch’s Phara.
So as part of it, our player has a rocket launcher which in next posts we will shoot rockets with!

Accordingly, public Transform rocketLauncher will help us later in the script to refrence the rocket launcher and change it’s rotation along the x axis so when the player rotate the camera, the rocket launcher will rotate too.

We are setting Cursor.lockState = CursorLockMode.Locked inside the start method. this will make the cursor invisible when we enter play mode.

We also have these two lines which will help us to set the rotation along the x axis of both the camera and the rocket launcher accordingly.

private float xRotationCamera = 0f;
private float xRotationRocketLauncher = 90f;

Update Method

We use these two lines in order to capture the mouse movement along the x and y axes accordingly.

float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

We then multiply each of them by mouseSensitivity which we have declared earlier.
We also multiplying it by Time.deltaTime in order to smooth the transition along the frames.

These lines will make the camera and the rocket launcher respectively rotate along the x axis while limiting the min and max angle of both.

xRotationCamera -= mouseY;
xRotationCamera = Mathf.Clamp(xRotationCamera, -90f, 90f);

xRotationRocketLauncher -= mouseY;
xRotationRocketLauncher = Mathf.Clamp(xRotationRocketLauncher, 0f, 180f);

Mathf.Clamp takes a value and limit it between a given range.
More on that can be found here.

In the following code we use the Quaternion.Euler(…) in order to take care of the rotation, first the camera’s and the the rocket launcher.
playerBody.Rotate(Vector3.up * mouseX) will make the player object rotate along the y axis while rotating the camera.

transform.localRotation = Quaternion.Euler(xRotationCamera, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
rocketLauncher.localRotation = Quaternion.Euler(xRotationRocketLauncher, 0f, 0f);

That’s it!

Camera Movement

Now, that we done with the code itself, we can assign the script to the camera object.
After that we set the mouse sensitivity, assign the player object in the Player Body reference and the rocket launcher in the Rocket Launcher reference.

After that we set the mouse sensitivity, assign the player object in the Player Body reference and the rocket launcher in the Rocket Launcher reference.

Now hit play and notice you can look around you using the mouse.
Also, when looking around, the rocket launcher is rotating with you!

Check out this guide, where we are adding player movement.

Leave a Reply

Your email address will not be published.