Camera Look System in Unity!

Objective: Control the camera with the mouse!

This tutorial we are going to complete the camera controller. We already have the camera following the player as they move through the world. Now we want to control where the camera is looking with the mouse.

We are going to be dealing with 2 new inputs: mouse X, and mouse Y.

We are going to take these values and apply them to the rotation on the y. First, we need a variable to represent the camera.

We will need this when looking up and down.

Let’s focus on left and right first. We need a local vector3 attached to the transform local euler angle of the player.

We want to set the y of the current rotation to the mouseX.

We can now take the current rotation and apply the local rotation to Quaternion angle axis.

We can now move on to looking up and down. We begin by creating a local vector3 and attaching to the camera’s local Euler Angles.

When we apply the mouseY, if we add the value the camera will be inverted (mouse up looks down, mouse down looks up). If we subtract the value, then mouse up will look up and mouse down will look down.

Apply the locadl rotation to Quaternion Andle axis with the local vector3 x, and Vector3.right.

The camera works perfectly! You will notice the player’s movements do not adjust to the camera. We will fix that in the next tutorial. Don’t be afraid to experiment with your code, and I’ll see you in the next Tutorial!