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?

CameraTestFinal.gif

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.

cameraRig3
The point above the player’s head is the FocusPoint, a child of the player object. The CameraPivot is a separate object that follows this point and also is the parent of the MainCamera.

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.

cameraRig4.png
This is the structure of my final camera rig. CameraPivot lerps towards FocusPoint, CameraSoftPos lerps towards CameraHardPos, and the CameraTransform is positioned between CameraSoftPos and the FocusPoint, accounting for wall clipping by raycasting between these last two.

Rig Hierarchy:

  •  PlayerTransform
    • FocusPoint
  • CameraManager
    • CameraPivot
      • CameraHardPos
    • CameraSoftPos
    • CameraTransform

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.

CameraTest4.gifNotice 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.

protectFromWallClipAnother 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.

Conclusions

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!

Advertisements

3 thoughts on “The secrets to building a smooth 3rd person camera Rig

  1. Nice work! It’s too bad it’s not getting used :(. I can’t seem to figure out the reason to have the CameraHardPos transform… It’s used in the sense that the CameraSoftPos is lerping toward it, but from what I read I interpreted that the CameraHardPos will always be at the same location as the CameraPivot. Am I missing something here?

    Thanks 🙂

    Like

    1. Hey Richard,
      Look at the picture for reference. CameraHardPos is always at the farthest possible point from the CameraPivot. Rotating the camera (with a controller for instance) means rotating the CameraHardPos around this pivot (CameraPivot). Camera SoftPos lerps to CameraHardPos. Having all these is not absolutely necessary, but in my experience it helped a lot to make it all work smoothly.
      I hope this helps!
      Good luck!

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s