Table of Contents

Example - Follow a Unit

You want the camera to track a moving target, fx. follow a unit across the map, or chase a vehicle.

SetFollowTarget hands the camera a Transform to follow. Calling it again with a different transform switches targets immediately; no need to clear first. ClearFollowTarget returns control to the player.

using UnityEngine;
using Goehler.OrbitalCam;

public class FollowSelection : MonoBehaviour {

    [SerializeField] private OrbitalCameraSystem _camera;

    public void Follow(Transform unit) {
        _camera.SetFollowTarget(unit);
    }

    public void Release() {
        _camera.ClearFollowTarget();
    }

    private void Update() {
        if (Input.GetKeyDown(KeyCode.Escape)) Release();
    }
}

Wire Follow(...) into your unit-selection code. When the player clicks a unit, pass its transform in. Click another unit, the camera switches. Press Escape to drop the target.

Note

Follow mode disables player movement input while active. Rotation, tilt, and zoom still work, so the player can frame the followed unit however they like; they just can't pan away from it. To pan away, clear the target first.

Note

Follow mode also overrides Follow Terrain Height. The camera's vertical position tracks the target's transform directly, which is usually what you want when following a unit that walks up and down hills.

Note

The follow target is not part of GetState. See Save and Load for how to persist it across saves.

You can try the follow toggle from the corresponding button in the Scripting Demo scene.