I’ve tried using free camera rigs available in the Unity asset store, however none of them were as robust as I wanted and most of the time the scripts were unoptimized and hard to read. So I decided to build my own from scratch.
The 3rd person camera rig can be very hard to program, it is arguably the hardest type of camera to make so how do we start?
Warning: This article may contain traces of programming and 3D terminology. From this point on I assume you have at least a basic notion of 3D space programming.
Building The Rig
The most basic way to achieve it is to make the MainCamera a child of the player object, this way it will follow the player and rotate with it. However, movement will feel very rigid and the camera rotation will be uncontrollable.
A better solution is to make an empty game object as a child of the player object to act as a FocusPoint for the camera. Make another separate empty game object CameraPivot to follow the FocusPoint around programmatically and make the MainCamera a child of the pivot. When the player inputs camera rotation in the controller stick, we rotate the pivot around the focus point.
//Move the camera pivot pivotTransform.position = Vector3.Lerp(pivotTransform.position, focusPoint.position, pivotMoveSmoothing); //Rotate the camera to face the player pivotTransform.rotation = Quaternion.Lerp(pivotTransform.rotation, Quaternion.LookRotation(Vector3.ProjectOnPlane((focusPoint.position - cameraTransform.position), pivotTransform.up), new Vector3(0f,1f,0f)), pivotRotationSmoothing); //Rotate the camera pivot around the focus point using input pivotTransform.RotateAround(focusPoint.position, new Vector3(0f,1f,0f), horizontalRotationSpeed);
Hot tip #1: By using linear interpolation (lerp) to move and rotate the pivot around the focus point, in addition to moving smoothly you will also find that the player will orbit the camera nicely when moving sidewards.
However, even that rig can be a bit hard to balance, especially if you want to implement smooth movement and wall clipping.
My best solution for a good smooth rig is to have an empty game object to mark the CameraHardPosition as a child of the CameraPivot instead of the MainCamera. This hard position moves abruptly to where the camera is supposed to go. Another object CameraSoftPosition moves softly towards CameraHardPosition. And finally the MainCamera is positioned between CameraSoftPosition and the FocusPoint, taking wall clipping into account.
While building such a rig you will have to constantly tweak and balance all the parameters, until it feels easy to control, smooth to turn, and comfortable to navigate.
Hot tip #2: Wall-clipping is handled more easily by using a sphere-cast instead of a raycast. Also, make sure that the camera moves instantly to avoid clipping but moves smoothly to restore it’s original position, this will feel much nicer.
Building a dynamic smart camera
In addition to having a camera that moves and rotates smoothly, you can also have a smart camera that is beginner friendly, prevents wall-clipping by avoiding corners and turns automatically to reveal important features.
Notice how the camera moves up as the player approaches the edge of the cliff. This gives a greater sensation of depth and height. Once the player moves away from the cliff the original camera pitch is restored.
However, it is important to prioritize user input if the player wants to move the camera around and look at different places. If not, it will give a sensation of not being in full control and feel uncomfortable to play.
Another nice feature is shown in this image where the camera avoids wall-clipping by moving away from the corners. The magenta lines are raycasts being fired from the player’s back, when they detect a corner the camera can move out of the way. This feels much better for casual gamers that are not used to controlling the camera all the time.
Again, if the player moves the camera this feature is temporarily turned off to give full control to the user.
There is no single solution to building a great 3rd person camera. Also you could simply buy a pre-made professional camera rig at the asset store. However, I am very satisfied with the results achieved with this method and I feel like understandig how the camera works is fundamental in order to adapt it later to fit the game perfectly.
There is no need to stop developing the camera where I did here in this article, many other features can be added to support the specific needs of your game. If you want to dig deeper there is a great talk by John Nesky, about the dynamic camera of the game Journey.
Let me know what you think about this method and if you have alternative solutions!
Good luck and go shake some cameras!