DEV Community

Garrett Burns
Garrett Burns

Posted on


Unity 3rd Person Camera Fundamentals Of 3rd Person Camera Control in Unity

I've been looking at Unity again for the first time in a while, and I've been trying to get a better understanding of the code in the fundamental elements like the camera. Camera control is essential to a smooth experience in a game. I looked at putting together a 3rd person camera that moves by mouse position, and have come to the necessities and quality of life features that go into a game camera.

Alt Text

The required pieces for the CameraController are a reference to the player object, a camera reference, variables to hold the camera's distance from the player as well as the mouse's movement. Sensitivity isn't needed but you'll want it in almost any case, and the same goes for min and max values for the Y angles to prevent the camera clipping under the ground.

Alt Text

The Start and Update functions are used to prepare the data necessary to move the camera in the LateUpdate function. The Start in this case is used to assign the "main" tagged camera to the cam variable, but the cam also could be a public property that is assigned in engine without any code. The Update here then reads for mouse input multiplied by the sensitivity values each frame. If you Debug.Log one of the GetAxis' like "Mouse X" you'll see that the number is at 0 when the mouse idle, at numbers like 0.025 when moving slowly, and at numbers potentially exceeding 1 when moving rapidly. These numbers will be used to calculate the camera's updated position in LateUpdate. The Y value is also clamped to the ranges determined at the top.

Alt Text

In the LateUpdate function, the camera takes these mouse inputs and moves the camera appropriately. The dir variable represents a distance using the property the component has set from the editor (though you could just put a number in the code for the same result), and the rotation quaternion holds a rotation using the values input by the mouse movement. These values are then applied the camera's position, using the player's position and adding the rotation numbers to angle it's direction and then multiplying the dir to extend the camera along the ray. The last line uses the Unity function LookAt targeting the player's position and facing the camera to the player.

With these elements tied together with some simple logic, you end with a rudimentary camera that can be expanded and streamlined pretty easily. From here you could give the camera zoom controls, a smooth cinematic pan, or toggle-able types like x-ray or night vision. Good camera controls are taken for granted, but people notice when things are wrong.

Top comments (0)

An Animated Guide to Node.js Event Loop

Node.js doesnโ€™t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.