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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Cinemachine Virtual Camera returns to old position from when it was last disabled

Discussion in 'Cinemachine' started by senabryer, Jan 9, 2023.

  1. senabryer

    senabryer

    Joined:
    Aug 20, 2015
    Posts:
    2
    I've got a 3rd Person Follow camera that I want disabled for certain areas in the game (in favor of other types) I currently have it so it switches between the 3rd Person Follow and a Dolly Camera seamlessly, but the problem is that when the 3rd Person Follow camera is disabled, it's locked into the last transform & rotation it was at before it was disabled. This is a problem because when the player returns to using that camera, they're likely moving in a different direction than they were before, so it's disorienting for it to swing 180 degrees around. (I've got videos below showing what I mean)





    I've been wracking my brain trying to think of a workaround to this, but I feel like there's got to be something within Cinemachine itself to solve this. I'm currently brute forcing the Virtual Camera's GameObject to stay behind the player's local Z axis when inactive with this code, but its position is completely ignored when the Virtual Camera takes priority:

    Code (CSharp):
    1. void LateUpdate()
    2.     {
    3.         if (currentCam != playerFollowCam)
    4.         {
    5.             playerFollowCam.transform.position = player.transform.position - player.transform.forward * 4;
    6.             playerFollowCam.transform.LookAt(player.transform.position);
    7.             playerFollowCam.transform.position = new Vector3(playerFollowCam.transform.position.x, playerFollowCam.transform.position.y, playerFollowCam.transform.position.z);
    8.         }
    9.     }
     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,267
    Did you try enabling InheritPosition on your FreeLook?

    upload_2023-1-23_4-56-24.png
     
  3. senabryer

    senabryer

    Joined:
    Aug 20, 2015
    Posts:
    2
    I had, but I wasn't using a FreeLook component but rather the standard VirtualCamera w/ the Body set at 3rd Person Follow that comes with Unity's 3rd Person Controller template. Enabled Inherit Position on that didn't change anything, but switching everything over to a CinemachineFreeLook solved the problem and it works just fine. Thanks!
     
    Paul_Hughes likes this.
  4. Paul_Hughes

    Paul_Hughes

    Joined:
    Sep 6, 2018
    Posts:
    33
    Exactly the same as me. I am using the CinemachineVirtualCamera with 3rd person follow and follow target as my player.
    When I teleport or respawn the camera is still facing the same direction pre-spawn.
    I have tried the ForceCameraPosition, SetPositionAndRotation methods, and my own implementations and work-arounds but to no avail, trying to get the Vcam or MainCamera to use the Start() values from where the vCam position started, or MainCamera.
    I can change the players position and rotation, as that can be changed no problem using Vector3 and Quaternion as you know, but the vCam just seems to stick to the same position just before the respawn, even after using the methods above there seems to be no change at all. I have tried inherit position as above also, but as you said, it's not the freelook camera, just the normal virtual camera, it was set up from the starter assets for the 3rd person.
    There must be something I am doing wrong if the ForceCameraPosition method isn't changing the vCam pos, or maybe it is and the camera somehow reverts to the values before, who knows?.
    I am using an IEnumerator for my re-spawning, was thinking maybe it had something to do with that, but it seems to complete it ok with the other statements etc., and gives a successful respawn, except the vCam the still face the same way, not behind the player, as I would like.
    I thought it was a rotation problem at first, but as I looked at the values properly, only the position changes on the vCam, but the rotation and pos does change on the MainCamera?


    So yeah, maybe I'm just too tired, or too old, I'm sure I will figure it out sooner or later.....maybe! lol, any help would be much appreciated, thanks.
     
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,267
    The way the 3rdPersonFollow component works is to position the camera based on the follow target's position and rotation. If you position and rotate the follow target, the camera will reposition and re-orient itself accordingly. ForceCameraPosition will do nothing, because the 3rdPersonFollow component will immediately move the camera back to where it's supposed to be. I can only surmise that you're not moving the follow target the way you think you are. A visual image or video of the problem would be helpful here, as well as an image of the hierarchy showing the camera, follow target, and main camera.