Day 131 — Creating a Camera Look System in Unity

Hey and welcome!

Now that we’ve got our player moving around we’re going to need to implement the logic to have our camera move along with our mouse position so that we can look about the game scene like a 3rd person game!

Let’s handle things one axis at a time first so we’ll start by adding in the following code which will give the player the ability to look left and right depending on the cursor’s position:

private void Rotation()
{
float mouseX = Input.GetAxis("Mouse X");
Vector3 currentRotation = transform.localEulerAngles; currentRotation.y += mouseX; transform.localRotation = Quaternion.AngleAxis(currentRotation.y, Vector3.up);
}

Input.GetAxis(“Mouse X) works similarly to its “Horizontal” counterpart as it returns a value between -1 and 1 depending on how you move your mouse along the x axis in your game. We’re then creating a new Vector3 variable that’s being assigned the x, y and z for localEulerAngles and it’s the transform.localEulerangles.y that we assign mouseX to when we do currentRotation.y.

We’re using a new method here called Quaternion.AngleAxis which rotates an angle a set amount of degrees you specify around the axis that you specify. In our case here the degrees we rotate by will either be -1 or 1 and we use Vector3.up to specify that we want to rotate around the y axis which will give us this:

Now let’s add in that code that lets us look up and down:

private Camera _mainCamera;void Start()
{
_mainCamera = Camera.main;
if (_mainCamera == null)
{
Debug.LogError("Main camera is null");
}
}
private void Rotation()
{
float mouseX = Input.GetAxis("Mouse X");
float mouseY = Input.GetAxis("Mouse Y");
Vector3 currentRotation = transform.localEulerAngles;
currentRotation.y += mouseX;
transform.localRotation = Quaternion.AngleAxis(currentRotation.y, Vector3.up);
Vector3 currentCameraRotation = _mainCamera.gameObject.transform.localEulerAngles;
currentCameraRotation.x -= mouseY;
_mainCamera.gameObject.transform.localRotation = Quaternion.AngleAxis(currentCameraRotation.x, Vector3.right);
}

As you can see here, instead of rotating our player directly this time we rotate the camera in order to allow usto look up and down since it wouldn’t make sense to rotate our player’s y axis and have them facing the floor or the sky when we need them facing the enemy!

One cool thing we’re doing to get a reference to our camera is by using Camera.main. This is something built into Unity and will grab whichever object is currently tagged as your main camera so there’s no need for us to assign it in the inspector.

Lastly you’ll notice that for currentCameraRotation.x I’m assigning it the negative value of mouseY. This is because using += will make the camera inverted, so when you move the cursor up you look down and vice versa.

With all that done you should have a pretty good look system in place now! You’ll notice however that when you move the player you’re not moving in the direction you’re looking at and this is because the player is moving in world space instead of local space. We’ll fix that in the next one and add some QoL changes to adjust the camera sensitivity and hide our mouse on the screen.

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.