Game.OrbitCameraController
Assembly:
Assembly-CSharp.dll
Namespace:
Game
Type:
class
Base:
UnityEngine.MonoBehaviour, IGameCameraController
Summary:
OrbitCameraController implements a player-controllable orbital camera used by Cities: Skylines 2 for general gameplay, photo mode and editor usages. It manages camera pivot, zoom, rotation, follow smoothing for tracked entities, collision restriction to terrain via a Cinemachine collider, and integrates with the game's audio and camera update systems. Initialization occurs during Awake (async waiting for game readiness), and the main per-frame logic is provided via UpdateCamera which the CameraUpdateSystem calls.
Fields
-
private static readonly float kPivotVerticalOffset
Used as a constant vertical offset (10 units) above terrain for the camera pivot so the pivot is kept above ground. -
public float2 m_ZoomRange
Minimum and maximum zoom range (default 10 to 10000). Zoom values are clamped to this range. -
public float m_FollowSmoothing
Smoothing factor controlling how quickly the camera catches up when following an entity. Smaller values produce slower (smoother) catch-up. -
private Entity m_Entity
Entity currently being followed. Backing field for the followedEntity property. -
private float m_FollowTimer
Timer used to influence smoothing when starting to follow an entity. -
private float2 m_Rotation
Internal storage for horizontal/vertical rotation in degrees (x = yaw, y = pitch). -
private GameObject m_Anchor
A GameObject created at runtime to act as the camera pivot/anchor that Cinemachine virtual camera follows and looks at. -
private CinemachineVirtualCamera m_VCam
Reference to the Cinemachine virtual camera component on the same GameObject. -
private CinemachineOrbitalTransposer m_Transposer
Reference to the CinemachineOrbitalTransposer component used to set follow offset (distance from pivot). -
private CinemachineRestrictToTerrain m_Collider
Component that restricts camera anchor and movement to terrain and handles collision clamping. -
private CameraInput m_CameraInput
Handles player input for camera movement, rotation and zoom. Initialized in Awake if present. -
private CameraUpdateSystem m_CameraUpdateSystem
Reference to the game's CameraUpdateSystem; the controller registers itself so UpdateCamera is invoked from that system.
Properties
-
public Unity.Entities.Entity followedEntity { get; set; }
Gets or sets the Entity being followed. Setting resets offsets and follow timer; when changed while enabled it refreshes the audio manager's followed entity. -
public Mode mode { get; set; }
Camera mode enum (Follow, PhotoMode, Editor). Public auto-property — used by game code to switch behavior or UI modes. -
public Vector3 pivot { get; set; }
World position of the camera pivot. Getter/Setter operate on the m_Anchor GameObject's transform.position. -
public Vector3 position { get; set; }
World position of the camera GameObject. Setting this also positions the anchor based on current anchor rotation and zoom (anchor placed behind camera along the local Z axis based on zoom). -
public bool controllerEnabled { get; set; }
Enable/disable the camera controller (toggles the GameObject active state). Getter returns isActiveAndEnabled. -
public bool inputEnabled { get; set; } = true
Enable or disable processing of player input. Defaults to true. -
public Vector3 rotation { get; set; }
Euler rotation reported as Vector3. Setter writes into internal m_Rotation (yaw, pitch mapping). -
public float zoom { get; set; }
Current zoom/distance value used by Cinemachine transposer offset. Values are clamped against m_ZoomRange in Awake and during update. -
public float yOffset { get; set; }
Vertical offset applied when following an entity (added in anchor positioning). -
public float xOffset { get; set; }
Horizontal offset applied when following an entity. -
public ICinemachineCamera virtualCamera
Read-only property exposing the Cinemachine virtual camera interface (m_VCam). -
public ref LensSettings lens
Returns a reference to the Cinemachine LensSettings for direct configuration of camera lens parameters. -
public bool collisionsEnabled { get; set; }
Gets/sets whether object collisions are enabled on the CinemachineRestrictToTerrain component. -
public Action EventCameraMove { get; set; }
Event invoked when the camera moves (used by UI or other systems to react to camera motion).
Constructors
public OrbitCameraController()
No explicit constructors are defined in the source. Initialization is performed in the async Awake method which waits for GameManager readiness and then creates the anchor GameObject and initializes Cinemachine and input references.
Methods
-
private async void Awake() : System.Void
Async initialization routine. Waits for GameManager.instance.WaitForReadyState(), creates the runtime anchor GameObject, gets Cinemachine components (virtual camera, orbital transposer, terrain restrictor), configures LookAt/Follow, disables the controller GameObject by default, initializes CameraInput if present, registers with CameraUpdateSystem, and clamps initial zoom to m_ZoomRange. -
private void OnEnable() : System.Void
Called when the component is enabled. Refreshes audio follow target to the currently followed entity. -
private void OnDisable() : System.Void
Called when the component is disabled. Clears the audio follow target. -
private void RefreshAudioFollow(bool active) : System.Void
Helper used by OnEnable/OnDisable and property setter to tell AudioManager which entity is being followed (or Entity.Null when not active). -
private void OnDestroy() : System.Void
Cleans up the runtime anchor GameObject and clears references in the CameraUpdateSystem. -
public void TryMatchPosition(IGameCameraController other) : System.Void
Attempts to match this camera's pivot/rotation/zoom to another camera controller. For CinematicCameraController, computes an appropriate pivot and zoom so the orbit camera matches the cinematic position while accounting for terrain height and the pivot vertical offset. For other controllers it copies zoom and pivot. -
public void UpdateCamera() : System.Void
Main per-frame camera update called by CameraUpdateSystem. Refreshes collider and input, reads CameraInput to adjust rotation, zoom and translation when not following an entity, clamps anchor to terrain, applies follow smoothing and offsets when following an entity, updates Cinemachine transposer follow offset (z), updates audio listener position/rotation, and invokes EventCameraMove when appropriate. -
private static bool TryGetPosition(Entity e, EntityManager entityManager, out float3 position, out quaternion rotation, out float radius) : System.Boolean
Utility to obtain a target entity's position, rotation, and approximate radius by querying SelectedInfoUISystem.TryGetPosition. Returns true if the entity had valid data; otherwise returns false and zeroes the outputs.
Usage Example
// Example: enable the orbit controller, set an entity to follow and switch to PhotoMode
var orbit = FindObjectOfType<Game.OrbitCameraController>();
orbit.controllerEnabled = true;
orbit.mode = Game.OrbitCameraController.Mode.PhotoMode;
orbit.inputEnabled = true;
orbit.followedEntity = someEntity; // Entity is from Unity.Entities
// The CameraUpdateSystem will call UpdateCamera each frame; you can also call it manually in tests:
orbit.UpdateCamera();
Notes: - This controller integrates with Cinemachine; ensure the CinemachineVirtualCamera and the custom CinemachineRestrictToTerrain/CinemachineOrbitalTransposer components are present on the same GameObject. - Zoom is applied by setting m_Transposer.m_FollowOffset.z to -zoom - radius so that object radius is taken into account when following entities. - The controller keeps the audio system up-to-date via AudioManager.instance.followed and AudioManager.instance.UpdateAudioListener.