Camera Anchor

By default, the Camera Controller will follow the “anchor” that is set in the inspector.

This anchor is typically the character and when the anchor moves, the Camera Controller moves (based on the motor).

For a motor like the 3rd Person Fixed motor, the “focus point” becomes the combination of the Anchor + the Anchor Offset properties. You can see that below:

The motor will then orbit around that focus point.

However, that means if there’s a small movement with the character, there’s a small movement with the camera.

Sometimes we want there to be a drag or delay in the camera movement. To do this, we change it so the anchor isn’t the character, but a third object. Will call this a “Camera Anchor”.

 

Camera Anchors

It’s a transform that can follow the character as loosely as you want and becomes the anchor the Camera Controller uses.

On the Camera Controller, you just set the Anchor property to your new Camera Anchor object.

At this point, you can add code to the Camera Anchor to move it however you want. This is just standard Unity code.

A simple example would just look like this:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
using UnityEngine;

public class SimpleCameraAnchor : MonoBehaviour
{
    public Transform Target = null;

    protected void Update()
    {
        if (Target == null) { return; }

        transform.position = Target.position;
        transform.rotation = Target.rotation;
    }
}

Here the camera anchor is just matching the position and rotation of a target (our character).

 

Basic Camera Anchor

Of course you can get a lot more fancy and my Basic Camera Anchor component does that.

It allows you to follow a target (the character) and have it’s own offset. You can also set the “Movement Lerp” in order to add some drag. This way the camera doesn’t move immediately with the character.

If needed, you can also freeze the position of the camera anchor. For example, this is a good option if you don’t want the camera to move up when the character jumps.

 

Custom Camera Anchors

In the end, you may want to create a custom camera anchor that behaves exactly how  you want.

If you look at the code for BasicCameraAnchor.cs, you’ll see some extra stuff that I add to link the camera anchor to my Actor Controller and Motion Controller. If you’re not using those, I’d say start from the code I provide above.

If you are using my Actor Controller or Motion Controller, you want to inject the camera anchor into my process flow. The easiest way to do that is to simply inherit from my BasicCameraAnchor class and then override the AnchorLateUpdate() function. This way, you’re moving the anchor after the character and before the camera.

 

Summary

In the end, we’re just disconnecting the Camera Controller from the character. This way, you can hijack the focus and move it where ever you want. You can even have it follow the character for a while, disconnect, and then re-follow.

You can control all this through a simple custom camera anchor.

 

Page Contents