[HOWTO] How To: Use Selector with Cinemachine

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
User avatar
Tony Li
Posts: 20703
Joined: Thu Jul 18, 2013 1:27 pm

[HOWTO] How To: Use Selector with Cinemachine

Post 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()
        {
        }
    }
}
Post Reply