Search Unity

Proper way to 'reset' follow camera?

Discussion in 'Cinemachine' started by AustinDrozin, Sep 18, 2019.

  1. AustinDrozin

    AustinDrozin

    Joined:
    Jun 8, 2016
    Posts:
    16
    Right now I have a camera that follows and aims at a 3rd person character controller. I have a function where the player can teleport back to a place they saved instantaneously. The problem is the camera kind of freaks out. It seems to be an issue with the Aim portion.

    I took out the art assets and character. Every time the camera rotates like mad is when I am pressing the teleport/return button. It just sends the sphere back to the starting position. I'd like the camera to reset back to a similar location as the start of the gif.



    How do I get this to just reset like normal similar to right at the beginning of the scene start?
     
  2. AustinDrozin

    AustinDrozin

    Joined:
    Jun 8, 2016
    Posts:
    16
  3. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,943
    It looks to me as though you're just warping the character to the start position. The camera is trying to follow, and because of the damping it takes time. You need to tell the camera to cancel the damping because this is a warp, and not just a fast movement that it has to follow.
    When you warp the player position, add this call: vcam.OnTargetObjectWarped(), to inform the vcam of the situation.
     
  4. AustinDrozin

    AustinDrozin

    Joined:
    Jun 8, 2016
    Posts:
    16
    oh so that function will turn off damping?
     
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,943
    For that frame, yes. It will preserve the target-to-vcam spatial relationship by applying to the vcam the same position delta as the target, bypassing all damping for that frame.

    Normally that call is used when you want a continuous-looking follow to happen across a warp of the target. A common use-case is infinite worlds when you wrap around.

    If you're trying to just start over, it might be easiest to disable the vcam re-enable it. That will cancel all previous state and make the vcam start over. While it's disabled, you can also reposition the vcam if you want to give it an initial starting point.
     
  6. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,943
    Correction: Disabling/re-enabling the vcam won't work because the vcam's OnEnable() method won't get called until the next frame. Try instead to reset the vcam by setting vcam.PreviousStateIsValid = false. That should make the vcam snap to the new spot.
     
  7. AustinDrozin

    AustinDrozin

    Joined:
    Jun 8, 2016
    Posts:
    16
    Hm I tried both ways, but nothing seems to be happening. Still does the same thing.

    Code (CSharp):
    1. vCam.OnTargetObjectWarped(t, posDelta);
    Code (CSharp):
    1. vCam.PreviousStateIsValid = false;
    podDelta is just subtracting the old position from the new position right?
     
    Last edited: Sep 29, 2019
  8. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,943
    Yes.

    Can you show me the entire code that handles the reset? vCam.PreviousStateIsValid = false should do the job.
     
  9. AustinDrozin

    AustinDrozin

    Joined:
    Jun 8, 2016
    Posts:
    16
    I commented out a function on the last line that called 'vCam.PreviousStateIsValid = false' in another class. That's all it does though.

    Code (CSharp):
    1. public void Respawn(Vector3 p, Quaternion r)
    2. {
    3.         Vector3 posDelta = p - transform.localPosition;
    4.  
    5.         transform.localPosition = p;
    6.         transform.rotation = r;
    7.         rb.velocity = new Vector3(0, 0, 0);
    8.  
    9.         vCam.PreviousStateIsValid = false;
    10.  
    11.         //camMovement.ResetCamera(transform, posDelta);
    12. }
     
  10. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,943
    Can you prove that vCam is actually the current active vcam? Also can you show me the inspector for the vCam, and for the CM Brain?
     
  11. AustinDrozin

    AustinDrozin

    Joined:
    Jun 8, 2016
    Posts:
    16
    Yes it is the only vCam in the scene.

    upload_2019-9-30_22-40-34.png
    upload_2019-9-30_22-40-57.png
     
  12. AustinDrozin

    AustinDrozin

    Joined:
    Jun 8, 2016
    Posts:
    16
    Need to see anything else?
     
  13. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,943
    It all looks good to me. I don't see off the top of my head why vcam.PreviousStateIsValid = false wouldn't work, if you're calling Respawn(). Can you see in the scene view that the player pops back to the origin but the vcam travels gradually? Does the effect disappear if you set the vcam's damping (all of it) to 0?
     
  14. AustinDrozin

    AustinDrozin

    Joined:
    Jun 8, 2016
    Posts:
    16
    So I set all the damping and look ahead to 0, but it doesn't go away completely. The rotation is still wonky, but only a tad. It doesn't reset completely and it still lerps over to the spawn point(not a warp).
     
  15. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,943
    So you're saying that with no lookahead and no damping (aim or body) you're not getting a hard follow? That's impossible. Turn off the noise too. If at that point the vcam isn't attached as though with a hard rod to your player, then there is something else interfering.
     
  16. AustinDrozin

    AustinDrozin

    Joined:
    Jun 8, 2016
    Posts:
    16
    upload_2019-10-3_9-5-20.png

    https://thumbs.gfycat.com/TheseAdorableBaiji-mobile.mp4


    Seems like the position damping makes the position a little better. Not perfect.

    The rotate is still pointing to the direction it was before the warp though. Would the only solution be to turn off all damping for one frame then reset the position of the vcam manually?
     
  17. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    2,943
    With those vcam settings, the camera should be 100% locked to the target - there should be no frame that doesn't have the target firmly positioned in the center of the game view.

    Yet, in your clip, there is this frame:

    upload_2019-10-3_9-19-43.png

    Conclusion: the camera target and the player are not the same game object. Camera target and player are not in the same position. Is that the case?
     
  18. AustinDrozin

    AustinDrozin

    Joined:
    Jun 8, 2016
    Posts:
    16
    Wow ok make me look dumb... lol. im ashamed

    I was totally overlooking the fact I changed the target to be a separate transform... That other cameraTarget is being lerped towards the player position.

    Thank you! It all works properly now.
     
    Gregoryl likes this.