Search Unity

Bug Cinemachine Raycasts incorrect when Framing Transposer is moving

Discussion in 'Cinemachine' started by gumboots, Nov 2, 2022.

  1. gumboots

    gumboots

    Joined:
    May 24, 2011
    Posts:
    298
    Hi all,

    I've got a top-down camera with Cinemachine using a Framing Transposer, and have found that if the camera is moving at all, raycast results become jittery/inaccurate even if the mouse is perfectly still.

    The only suggestion I've found is from @Gregoryl to make sure casts are being done after Cinemachine has updated. However I am already manually updating Cinemachine with CinemachineBrain.ManualUpdate() in LateUpdate. After which I then run the following code:

    Code (CSharp):
    1. Ray ray = WorldCamera.Camera.ScreenPointToRay(screenPoint);
    2. new Plane(Vector3.up, new Vector3(0, 1.5f, 0)).Raycast(ray, out float enter);
    3. Debug.Log(ray.GetPoint(enter));
    With the mouse perfectly still on screen, the result of ray.GetPoint(enter) jitters for a long time if the Cinemachine camera has moved at all recently (even very, very slightly), and continues to jitter for a few seconds after "reaching" its target (though looking at the VirtualCamera's transform, the camera itself is also jittering very slightly). Incidentally, with Gizmos drawing in the Game view, I can also see the Cinemachine frustum shaking in the same manner (and lining up perfectly with the raycast's jitter).

    Any help is greatly appreciated, as I've been stuck on this for a long time!

    And in case it helps, here are my Cinemachine settings (the VirtualCamera's Transform.rotation is just set to X=50):


     
    Last edited: Nov 2, 2022
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    It sounds like a feedback problem due to incorrect parenting. Can you show your hierarchy?
     
  3. gumboots

    gumboots

    Joined:
    May 24, 2011
    Posts:
    298
    Thanks for the reply! You can, but as far as I can tell all of the important/relevant Transforms are at the root:

    CameraTarget's position is set directly in my script, and I've tried setting that in Update, LateUpdate and FixedUpdate with no change.
     
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Yes, the hierarchy looks good. If you're setting camera target's position in response to camera movement, and camera movement uses camera target position as input, then that is another opportunity for feedback. Could that be what's happening?
     
  5. gumboots

    gumboots

    Joined:
    May 24, 2011
    Posts:
    298
    I did have that thought, because I was setting the distance from the player based on the mouse's viewport position. So I stripped it back to be as basic as possible:
    And have the same results as above. The camera position isn't being affected at all by the mouse position, just the target's position.

    When the camera is still, the returned position is steady as a rock. However if I move the camera, during the transition it flickers in a small area around the cursor, and carries on flickering for a period even after the camera appears to have stopped visually. However its Transform is ever slightly jittering, and I can see the camera frustum jittering as well in the Game scene Gizmos.
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Can you send me a test project that reproduces this issue?
     
  7. gumboots

    gumboots

    Joined:
    May 24, 2011
    Posts:
    298
    Apologies for the delay!
    Below is a package that reproduces the issue. You'll obviously need Cinemachine in the project, then:
     
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,724
    Found the problem: your near clip distance was too small. Projection matrices become more inaccurate as the near clip distance approaches zero. Changing it from 0.001 to 0.01 fixed the problem.
     
  9. gumboots

    gumboots

    Joined:
    May 24, 2011
    Posts:
    298
    Thanks very much for your help!