Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Cinemachine v1.1

Discussion in 'Cinemachine' started by Adam_Myhill, Jan 5, 2017.

  1. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,972
  2. thieum

    thieum

    Joined:
    Apr 8, 2011
    Posts:
    54
    @Adam_Myhill, @Gregoryl : does CM require a special raycast syntax of its own?
    After using Cinemachine with Timeline, I'm now wondering if I should start using them outside, as to eventually replace my usual camera scripts.
    But then I realised that there's an issue with a simple raycast. The returned ray has wrong position and direction when Cinemachine is in use, compared to what you would expect from :
    Code (CSharp):
    1.  
    2.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    3.             if (Physics.Raycast(ray, out hit))
    4.             {
    5.                 clickPosition = hit.point;
    6.                 Debug.DrawLine(Camera.main.transform.position, clickPosition, Color.red, 1.0f);
    7.                 Debug.Break();
    8.             }
    9.  
    Obviously, in the tested situation the camera transform does match the virtual camera one, as shown in the shot, and the red line comes from the code above, when I click near the white ball...
    Turning off CM and the raycast works as intended.
    I can't get what's going on, really...

    cinemachineRaycastIssue.jpg
     
  3. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,972
    Cinemachine is just a way of animating the Camera that has the CM Brain. Think of it as a fancy animation tool. The camera gets positioned in LateUpdate. Raycasts should work perfectly normally.

    Is it possible that you have more than one Camera in your scene, and that Camera.main is not actually the active camera?
     
    thieum likes this.
  4. thieum

    thieum

    Joined:
    Apr 8, 2011
    Posts:
    54
    @Gregoryl Thank you for your reply.
    I tested other scenarii by aligning CMBrain update mode and my raycast function, either LateUpdate / FixedUpdate / (smart) Update.
    I got expected results with both passed in LateUpdate() and FixedUpdate(). Smart Update() still returns wrong main camera position. Guess it is updating only occasionally, on event call, isn't it?

    But I wonder where the main camera transform is supposed to be in between, for instance when I test without moving it. From the picture above, we could figure out that the returned camera position is a cached one, or the actual one without followOffset applied, or alike...

    It seems useful to point out such corner cases, when grabbing main camera position in a different event function.
    Thanks to your input, there's a workaround. I'm still interested in using Cinemachine for interactive cameras (as in the camera-control-character scheme) as well.
     
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    3,972
    Setting the brain to Late/Fixed/Smart affects when the vcams track their targets and evaluate their own positions. Regardless of that setting, the Unity Camera itself is always updated in CinemachineBrain.LateUpdate(). That is when the vcam's position is applied to the Camera. CM does not move the camera at any other time, so it should remain at the previous frame's position unless you have some script that is affecting the Camera's transform. Is it possible that you're somehow moving the Camera before CM Brain's LateUpdate is called?

    Ideally, your camera should be a top-level GameObject, not parented to anything, with no scripts that might alter its transform, other than CM Brain.
     
    Last edited: Nov 16, 2018
    thieum likes this.
unityunity