Skip to main content

Class SwitchableCameraController

Owns the active Cinemachine virtual camera and toggles between first-person and third-person views in response to event-aggregator messages.

[RequireComponent(typeof(CinemachineBrain), typeof(Camera))]
public class SwitchableCameraController : MonoBehaviour, IHandle<SwitchableCameraMessage.CameraViewTypeMessage>

Inheritance​

Implements​

IHandle<SwitchableCameraMessage.CameraViewTypeMessage>

Examples​

Publish CameraViewTypeMessage through the event aggregator from any system — a UI button, a scenario trigger, or an input handler — to switch perspectives without holding a direct reference to this component:

// In a UI button's OnClick handler or a scenario script:
if (UnityServiceLocator.Current.TryGet(out var aggregator))
{
// Switch to first-person view when the participant enters the vehicle.
aggregator.Publish(new CameraViewTypeMessage(CameraViewType.FirstPerson));
}

The same aggregator call works from any MonoBehaviour in any scene; the controller unsubscribes itself in OnDestroy so there is no risk of a stale handler. To read the current view type without publishing a message, query CurrentCameraViewType directly on the component:

var controller = Camera.main.GetComponent();
bool isFirstPerson = controller.CurrentCameraViewType == CameraViewType.FirstPerson;

Remarks​

View changes are driven via IHandle rather than direct method calls so that any game system (UI button, scenario script, editor shortcut) can switch the camera without holding a reference to this component.

FirstPersonCullingLayerMask hides the player vehicle body in first-person, preventing z-fighting and the player seeing the inside of their own car mesh. Because the Cinemachine blend is not instantaneous, culling is applied only after the transition coroutine completes so the body does not pop during the blend.

Fields​

FirstPersonVCam​

First-person virtual camera used in first-person view.

[Header("First Person Properties")]
[Tooltip("Cinemachine virtual camera to use for first-person view.")]
public CinemachineVirtualCamera FirstPersonVCam

Field Value​

CinemachineVirtualCamera

ThirdPersonVCam​

Third-person virtual camera used in third-person view.

[Header("Third Person Properties")]
[Tooltip("Cinemachine virtual camera to use for third-person view.")]
public CinemachineVirtualCamera ThirdPersonVCam

Field Value​

CinemachineVirtualCamera

Properties​

CurrentCameraViewType​

Return the camera's current view type

public CameraViewType CurrentCameraViewType { get; }

Property Value​

CameraViewType

Methods​

Handle(CameraViewTypeMessage)​

Handles a direct camera view change request.

public void Handle(SwitchableCameraMessage.CameraViewTypeMessage message)

Parameters​

NameTypeDescription
messageSwitchableCameraMessage.CameraViewTypeMessageMessage containing the new view type.

SetCameraView(CameraViewType)​

Activates the appropriate virtual camera and starts the culling coroutine.

public void SetCameraView(CameraViewType newCameraViewType)

Parameters​

NameTypeDescription
newCameraViewTypeCameraViewTypeThe view type to switch to.

ToggleCameraViewType()​

Toggles between first-person and third-person camera views.

public void ToggleCameraViewType()