Camera Controller | Unity3D

Camera Controller Example

When it comes to game design creating a great camera controller can really affect the outcome of your game! When I started designing my third-person online game, I knew that my scripts had to stay simple so that I could later add the networking without issues. Remember you don’t always have to over complicate everything!

So before I started I knew that I needed three features to ensure it came out right;

1) Lerp Back to Rest Point
2) Adjustable Distance
3) Realistic Clamps

First I start off by naming my variables, adding the speed variables for the rotation and lerp speed. We add our clamp values and last but not least the references we need to differentiate our current angle with our rest angle, so when they are not matching and we are not moving the camera, we can call for a nice smooth lerp back to the rest point. Lastly, we add our variables for Cameral Scroll and references!

I have added a public bool called autoRest, which is responsible for activating the automatic lerp back to the rest point. If your character is in a stance like combat mode, where you do not want the camera to move, then we just turn this bool on!

Once we have our variables set up, I began to add my functions to update and added the script to move our camera with our target/player. Let me explain what these functions are supposed to do;

ControlCam(): This function is what controls the movement of your camera. Here we grab the Mouse X-axis and Mouse Y-axis and turn that into a float. Based on that we can get our new rotation by adding our new values to the X and Y rotation of our current rotation. Before we apply our new rotation we must use ClampAngle to set the max X clamp.

GetLookDirection(): This function is responsible for detecting if you are looking left or right, this is something you can use later on to help you with animations or other functions needed! This is all detected from the booleans in the variables LookRight and LookLeft.

ViewDistance(): Lastly this function will handle the mouse scroll zoom in and zoom out!


First, we check if our autoRest boolean is set to true or false, if not then we can start the portion of the script that includes the lerp back to rest point. We grab the mouse axis as explained above and send it through our clamp angle helper!

With the returned value we can now set our rotation and set our camRotate bool to true, once we let go of the mouse we set camRotate to false.

We want to start our lerp once our camRotate is set to false, so we check if the boolean is not true, if the transform rotation is not equal to our restAngle and if our Vertical axis is zero. The vertical axis is checking if our player is moving, so during running or walking our camera won't be moving too much.

If autoRest is set to true we can then automatically have our camera follow our mouse, without clicking. This works best for combat modes but the idea is easily switching between two styles of camera control, based on your game.


This is a pretty interesting script that I added to help with testing, animations, or anything I want to implement based on the look direction of the player. It took me quite a while to get the rotations correct but turning the current camera rotation to localEulerAngles.y did the trick. I based that on in-game testing with looking left and right and got the best values to check.

Now when your player is looking between 60–180 degrees they will be looking right, if they are looking between 200–315 degrees they are looking left. Else, they are looking straight. This was pretty confusing to me but you know what, it worked, sorry if I don't explain this the best!


For the final function, we add our ViewDistance, first, we check if our camera is orthographic or not so regardless of what camera setup we have scrolling should work.

How we made this work is by adjusting the fieldOfView or OrthographicSize base on scroll direction and zoomSpeed.

The Editor

Now before I leave! There is a certain way this is meant to be set up in the editor for it to work.

Player: Inside of your player add an empty Gameobject, this is what the camera will be following so center it as best you can.

Camera: Your script needs to be a parent of your Main Camera, your camera will be controlled from this source.

Place your target as cameraRot and for best results, match my values!

Part 2 — Player Controller

Download Source

If you want to download the Unity3D package you can download it on my Patreon! Thanks for reading and I hoped I helped some people learn something new and save some time :]


Indie Game Developer

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