Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Can I know when the camera is moving or idle?

Discussion in 'Cinemachine' started by juavazmor, Dec 5, 2022.

  1. juavazmor

    juavazmor

    Joined:
    Mar 8, 2021
    Posts:
    14
    Hi there!

    I'm using a CinemachineFreeLook with a Follow and LookAt targets with 3 orbits and smooth movement when rotating around the target.

    I'm in the need of knowing when the camera is moving (not just when I am moving the mouse, but also when I stop moving it and it continues moving due to the smooth movement of the component) or if it has reached its destination (actually stopped). Is this possible?

    Thanks in advance
     
  2. IndieForger

    IndieForger

    Joined:
    Dec 31, 2012
    Posts:
    92
    I have only started to use cinemachine days ago so not sure if there is actually flag that would allow you to test for it but it would be straightforward to implement that check. You need the custom script that you could attach to the object with camera brain that would keep track of properties you need to test for.

    In your case that would be transform position and rotation (and camera fov perhaps if you want to test for zoom).
     
  3. IndieForger

    IndieForger

    Joined:
    Dec 31, 2012
    Posts:
    92
    Took a coffee break to test it. Here is the code you need.

    Code (CSharp):
    1. [ExecuteInEditMode, RequireComponent(typeof(Camera))]
    2. public class CameraStatus : MonoBehaviour
    3. {
    4.     private Camera m_Camera;
    5.     private Quaternion m_LastRotation;
    6.     private Vector3 m_LastPosition;
    7.  
    8.     private bool m_IsIdle = true;
    9.     public bool isIdle { get => m_IsIdle; }
    10.  
    11.     private void OnEnable()
    12.     {
    13.         m_Camera = GetComponent<Camera>();  
    14.  
    15.         TrackCameraValues();
    16.     }
    17.  
    18.  
    19.  
    20.     private void Update()
    21.     {
    22.         m_IsIdle = IsIdle();      
    23.         TrackCameraValues();
    24.     }
    25.  
    26.     private bool IsIdle()
    27.     {
    28.         return Vector3.Equals(m_Camera.transform.position, m_LastPosition) && Quaternion.Equals(m_Camera.transform.rotation, m_LastRotation);
    29.     }
    30.  
    31.     private void TrackCameraValues()
    32.     {
    33.         m_LastRotation = transform.rotation;
    34.         m_LastPosition = transform.position;
    35.     }
    36. }
    37.  
    Simply attach it to the camera component.

    Added
    ExecuteInEditMode
    so it actually works in the editor, even when you move the camera manually. With above it doesn't matter what is the source of the change, virtual camera, another script or manual change.

    Just use
    isIdle
    getter to test if camera is idle or not.

    Hope that helps.
     
    Gregoryl likes this.
  4. IndieForger

    IndieForger

    Joined:
    Dec 31, 2012
    Posts:
    92
    Also... went one step further and created simple Editor script.

    Code (CSharp):
    1. #if UNITY_EDITOR
    2. using UnityEditor;
    3. [CustomEditor(typeof(CameraStatus))]
    4. public class CameraStatusEditor : Editor
    5. {
    6.     new private CameraStatus target { get => base.target as CameraStatus; }
    7.     TextField m_TextField;
    8.     public override VisualElement CreateInspectorGUI()
    9.     {
    10.         VisualElement root = new();
    11.         m_TextField = new() { label = "Status" };
    12.         m_TextField.SetEnabled(false);
    13.         root.Add(m_TextField);
    14.         EditorApplication.update += Update;
    15.         root.RegisterCallback<DetachFromPanelEvent>(c => {
    16.             EditorApplication.update -= Update;
    17.         });
    18.         return root;
    19.     }
    20.  
    21.     void Update() => m_TextField.value = target.isIdle ? "Idle" : "Moving";
    22. }
    23.  
    24. #endif
     
  5. IndieForger

    IndieForger

    Joined:
    Dec 31, 2012
    Posts:
    92
    It's not super advanced but give you real-time like update in editor. Just either drop it into the Edtor folder or attach it to the top of previous script (just after initial "using" statements).

    Hope it helps a bit. Enjoy!
     
  6. juavazmor

    juavazmor

    Joined:
    Mar 8, 2021
    Posts:
    14
    Wow thanks a lot @IndieForger!! I was thinking about doing a similar approach but just wanted wanted to be sure if Cinemachine had something similar already working before trying the solution by myself.

    The Editor script is more than welcome as well!

    All the best!