Nerd For Tech
Published in

Nerd For Tech

Creating a Look Camera System

Objective: Allow the player to manipulate the follow camera.

We’ll begin by setting the camera as a child object to our player object, then setting the desired camera angle.

In our Player script, we’ll need a few variables. The _min/_maxCameraXRotation variables will limit how low of high the player can move the vertical camera angle. We’ll use _isLookUpDownInverted to allow the player to customize how the camera will react to the mouse moving up or down, while _mouseSensitivity is, well, how sensitive the mouse will respond to movement. We should strive to find ways to enable our players to customize their experience!

When the game begins, we’ll “lock” the cursor to the center of the screen, which will also cause the cursor to vanish.

Still, it is a good practice to give the player control over their own system, so we’ll let them press the Escape key to get their cursor back at any time.

Our CameraController method will be called every frame. First, we’ll get the player’s mouse input.

Then we’ll process their horizontal mouse input. Their input will be multiplied by the _mouseSensitivity setting and applied to the player object’s localRotation. This has the effect of turning the player within the game world, allowing them to turn while walking forward.

Finally, we’ll process the player’s vertical mouse input. We’ll get the camera’s current rotation. Then we’ll apply the mouse movement to the rotation’s x parameter, but we’ll use Mathf.Clamp to make sure the player can’t look down too low (so they can’t shoot themselves) or up too high. We’ll use _isLookUpDownInverted to determine if we should add or subtract the input, based on player preference. Finally, we’ll apply the new rotation back to the camera.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store