Page 1 of 1

[HOWTO] How To: Use Selector with Cinemachine

Posted: Wed Jan 11, 2023 10:58 am
by Tony Li
If you're using Cinemachine and a Selector component, use the subclass below instead of Selector.

When using Cinemachine, the camera is not stable across Update() calls, so raycasts will not be accurate. This subclass changes the selector to use CinemachineCore.CameraUpdatedEvent to run the raycast instead.

CinemachineSelector.cs

Code: Select all

using Cinemachine;
namespace PixelCrushers.DialogueSystem
{
    public class CinemachineSelector : Selector
    {

        protected override void OnEnable()
        {
            base.OnEnable();
            CinemachineCore.CameraUpdatedEvent.AddListener(OnCameraUpdated);
        }

        protected override void OnDisable()
        {
            base.OnDisable();
            CinemachineCore.CameraUpdatedEvent.RemoveListener(OnCameraUpdated);
        }

        private void OnCameraUpdated(CinemachineBrain cinemachineBrain)
        {
            base.Update();
        }

        protected override void Update()
        {
        }
    }
}