Creating a Camera Look System in Unity!

Marcus Ansley
3 min readAug 12, 2021

--

After setting up a character controller for use in a Unity 3D project, I positioned the main camera and made it a child object of the Player. With this done, let’s set up the camera look functionality in a dedicated CameraLook method on the Player class 😉

1. In the Player class, create a new ‘CameraLook’ method. What’ll be driving this behaviour is our Mouse X and Mouse Y inputs, so we’ll be accessing both of these values and storing them in floats:

2. Next, let’s try to change the player’s rotation. Since this Player script is on the player object itself, we can store their current rotation simply by accessing ‘transform.localEulerAngles’ (returning a Vector3). For our player’s rotation, we’re going to want to access their Y axis, then add our mouseX variable to it. We could then either change the player’s localEulerAngles with this new Vector3 or we could change their localRotation (using a Quaternion). The Quaternion.AngleAxis method can let us rotate the player on a given axis — we just need to supply the angle of the rotation (our newPlayerRotation.y value) and the axis (in this case, Vector3.Up). Let’s take a look at how this is looking:

This should allow our mouseX value to drive the player’s rotation, and you’re welcome to test this in Unity now if you’d prefer. Otherwise, let’s move onto changing the camera’s rotation.

3. Just as we did with the player’s rotation, let’s define a new Vector3 and call it something like ‘newCameraRotation’. Instead of assigning the player object’s localEulerAngles to it, we’re going to want to access and use Camera.main.transform.localEulerAngles (or you’re going to want a Camera or GameObject variable for the camera for us to work with).

Since we’re changing the camera’s look up and look down movement, it’ll be our camera’s X rotation we’ll want to change. We can now either add our player’s mouseY value to this axis or we can subtract it; the former will give us inverted camera movement (i.e. down on the mouse = look up) while the later will give us what’s more conventional (i.e. down on the mouse = look down).

One extra step before we change the camera’s localRotation is that we may want to clamp this newCameraRotation.x value between, say, 0 and 26 (using Mathf.Clamp). If you’re not too worried about restricting the player from looking too high or too low, then you’re welcome to leave this step out.

As the last step for setting the camera rotation, assign to the camera’s transform’s localRotation ‘Quaternion.AngleAxis(newCameraRotation.x, Vector3.right)’. This is telling the camera to rotate by the degrees specified in newCameraRotation.x on an axis of Vector3.right (i.e. on a horizontal axis).

Our current CameraLook method should now be looking something like this:

4. Lastly, make sure to call this CameraLook method in the Update method. With this all set up, you should now be able to head back over into Unity, hit Play, and start controlling your camera and player character using your mouse input 😉

--

--

Marcus Ansley
Marcus Ansley

Written by Marcus Ansley

Game Developer | Game Design and Literature Graduate

No responses yet