Search Unity

Question [SOLVED] Raycast and Position delay

Discussion in 'Cinemachine' started by Raided, May 24, 2022.

  1. Raided

    Raided

    Joined:
    Dec 11, 2016
    Posts:
    7
    So I'm using the ThirdPersonController from starter assets which has the Cinemachine camera tracking a pivot point on the player Object. Now I want to make the player aim with the camera movement. The problem is the raycast information from the camera position is always a frame behind since the camera is LateUpdate.

    I can use the OnCameraUpdated event to get the new position and do the rotation after the camera, however then the animation rig is out of sync and still stuttering.

    I'm kind of stuck and reaching out for ideas.
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    I'm a little confused by this. What rotation are you doing here? Once OnCameraUpdated is called, the camera and player have all been positioned, and you can then safely do a raycast from the camera and have it land correctly.
     
  3. Raided

    Raided

    Joined:
    Dec 11, 2016
    Posts:
    7
    So input rotates the player. The animation rig is tracking an object that is moved via a raycast from the camera. However since the camera is LateUpdate (rotates with the player), the raycast information the animation rig is using is old.

    animation rig tracking object in Update()
    Camera information is LateUpdate()

    This misalignment is causing the multi-aim constraint to stutter as the raycast is not being updated smoothly. Hopefully, that makes sense.
     
  4. Raided

    Raided

    Joined:
    Dec 11, 2016
    Posts:
    7
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    Thanks, I understand.

    I can think of these possible solutions:
    1. Apply the aiming animation in the OnCameraUpdated callback instead of Update.
    2. Set the CMBrain to ManualUpdate mode, and call CMBrain.ManualUpdate() before the aiming animation is applied. This will update the vcam state but not the main camera. You'll have to use a dummy (disabled) Camera object to do the raycast, using position/lens info extracted from vcam.State.
    3. Fake it. Aim towards a fixed point along the player's +z axis, but make the bullet go to the correct place when it fires.
     
  6. Raided

    Raided

    Joined:
    Dec 11, 2016
    Posts:
    7
    The ManualUpdate works perfectly thank you, though I didn't end up using a dummy. I'm calling ManualUpdate in both Update() and LateUpdate(), which I think is worth the extra calculation.
     
  7. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    I don't think you should call ManualUpdate more than once per frame. It's asking for trouble.