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. Dismiss Notice

How To Reset The Cinemachine Virtual Camera Transform Through Script?

Discussion in 'Cinemachine' started by SFlowers, Apr 11, 2019.

  1. SFlowers

    SFlowers

    Joined:
    Apr 11, 2019
    Posts:
    1
    I tried to do something like vCam.GetComponent<Transform>().position = new Vector3(0,0,0);
    but the values keep the same, i also disabled the cinemachine virtual camera script but did not helped me.

    Here is the area where i wanted to reset.

     
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    Since you don't have a follow target in the vcam, it is possible to set the position. Be aware, however, that as soon as you assign a follow target and thus enable the procedural positioning, the vcam will immediately overwrite whatever position you try to set manually.

    normally, the way to do it is just to say vcam.transform.position = bla.
     
    Gerzent likes this.
  3. inear_sthlm

    inear_sthlm

    Joined:
    Sep 14, 2015
    Posts:
    4
    Old question, but ran into the same issue so posting my solution if someone else search for one. I have follow target , aim target, follow-orbital-transposer and aim-composer. I needed to reset that camera so a transition back again would be the same start values. I did like this:

    vcam.enabled = false;
    vcam.transform.SetPositionAndRotation(startPosition, startRotation);
    vcam.enabled = true;
     
    KolonelKitten, RGBit and EdwinLyons like this.
  4. HavanaBanana

    HavanaBanana

    Joined:
    Jul 3, 2017
    Posts:
    4
    Hi guys,

    i have the same issue. I cannot reset the rotation of my thirdperson vcam (body: framing transposer; aim: do nothing; this camera is a child of my player-gameobject). I have a bunch of cameras like menu-cam, aim-cam, and death-cam with different values.

    What I simply want to do:

    When a player dies, the deathcam (body: orbital transposer; aim: composer) gets activated by increasing the priority. When the player respawns, the regular thirdperson-vcam needs to be re-activated. And it does by setting priorities to values before. But the thirdperson vcam keeps the rotation of the deathcam. The solution above is not working for me.
    I tried to set the transform of my vcam-gameobject and the vcam itself. i also tried nulling the lookat and follow before resetting the rotation to avoid procedural positioning.

    What am I missing?
     
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    Unparent your cameras from the target game object. Main camera and vcams should be separate game objects, not parented to each other or anything that's moving.
     
    happybara likes this.
  6. Gunar

    Gunar

    Joined:
    Jun 11, 2013
    Posts:
    1
    CinemachineFramingTransposer transposer = cam.GetCinemachineComponent<CinemachineFramingTransposer>();
    transposer.ForceCameraPosition(Vector3.zero, Quaternion.identity);

    this will reset position and rotation without interpolation
     
    Kaldrin, Curipoc and alpdogan like this.
  7. Kaldrin

    Kaldrin

    Joined:
    Jul 10, 2018
    Posts:
    42
    THANK YOU AFTER HOURS OF RESEARCH THIS IS THE SOLUTION
     
    Last edited: Nov 16, 2021
  8. rmolina3d

    rmolina3d

    Joined:
    Nov 19, 2021
    Posts:
    1
    Aloha, I have a setup where a freelook cinemachine camera is orbiting around the player, when I pull the aim bow, it turns off the freelook camera and main camera switches to the 1st person camera... im doing this by deactivating the freelook camera, but when I re-enable the freelook camera itt goes back to the position it was before the deactivation, this is wonky if i wasnt looking in same direction as the 1st person camera.... im doing this becouse for some reason I cant get the main camrea to do the switch any other way... im having to use the cusom switcher under the main camera.... all in all im using a freelook and a virtual camera, and this is the only way I can get them to switch, I tried altering the cameras priorities vaules but for some reason the main camera will not do the switch unless the freelook is deactivated...... SO im trying to go from a free look to a virtual camera, and when the virtual camera releases go back to the freelook camera, which I need set pointing in the same direction as the virtual camera..... I hope I described my problem and what im trying to do right...
    thanks for any and all reasponses.....
     
  9. CiscoA

    CiscoA

    Joined:
    May 3, 2017
    Posts:
    2
    For anyone on void you can use cinemachineVirtualCamera.ForceCameraPosition(startPos, Quaternion.identity); to update the camera.
     
  10. Kaldrin

    Kaldrin

    Joined:
    Jul 10, 2018
    Posts:
    42
    This a complicated description of a simple problem but I think you can use what others suggested to move the freelook camera in the right orientation

    cinemachineVirtualCamera.ForceCameraPosition(startPos, Quaternion.identity);
     
  11. unity_AADFAE8DE8B569FBCF2C

    unity_AADFAE8DE8B569FBCF2C

    Joined:
    Feb 18, 2022
    Posts:
    1
    Why?
     
  12. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    Because when it has procedural motion enabled the vcam will track the target, then the main camera with the CMBrain will track the vcam. There is no need to parent. If you do parent, it is easy to inadvertently set up a feedback loop like a dog chasing its tail. It's best practice to keep things as clean as possible.
     
  13. miguelvvplay321

    miguelvvplay321

    Joined:
    Jul 14, 2021
    Posts:
    2
    I have a bug in camera 2, when I switch from camera 1 to camera 2 reducing its priority camera 2 is not seen, using the vcam2.transform.position = bla.
    I use the j key to transform the position of camera 2.
    Use the k key to set the camera priority 1 to 0
    Use the l key to set the camera priority 1 to 11
    Here is my code and the image of the error

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Cinemachine;
    5.  
    6. public class Prueba : MonoBehaviour
    7. {
    8.     public CinemachineVirtualCamera vcam1;
    9.     public CinemachineVirtualCamera vcam2;
    10.  
    11.     void Update()
    12.     {
    13.         if (Input.GetKeyDown(KeyCode.J))
    14.         {
    15.             vcam2.transform.position = new Vector2(17.33f, 19.30f);
    16.         }
    17.         if (Input.GetKeyDown(KeyCode.K))
    18.         {
    19.             vcam1.Priority = 0;
    20.         }
    21.         if (Input.GetKeyDown(KeyCode.L))
    22.         {
    23.             vcam1.Priority = 11;
    24.         }
    25.     }
    26. }
    Demonstration of the problem
    https://imgur.com/ChFRau3
     
    Last edited: Apr 16, 2022
  14. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    That's weird. Does it happen if you put vcam2 somewhere else?
     
  15. MaximilianPs

    MaximilianPs

    Joined:
    Nov 7, 2011
    Posts:
    321
    I have a similar problem, I'm working on a click to move game, and to avoid the camera from colliding with terrain I'm using the Collider Extension. The idea is to reposition the camera when it's free again, sadly the component haven't any Event to know when it's colliding.
     
  16. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    Try polling CinemachineCollider.CameraWasDisplaced() to see if camera is colliding.
     
    MaximilianPs likes this.
  17. MaximilianPs

    MaximilianPs

    Joined:
    Nov 7, 2011
    Posts:
    321
    That's really useful thanks :D
     
    Gregoryl likes this.
  18. Proto-G

    Proto-G

    Joined:
    Nov 22, 2014
    Posts:
    213
    This is what I used
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Cinemachine;
    5.  
    6. public class CineReset : MonoBehaviour
    7. {
    8.     void Start()
    9.     {
    10.         // ***Place this script on your Cinemachine***
    11.        
    12.         // Reset your Cinemachine to 0, 0, 0
    13.         this.enabled = false;
    14.         this.transform.SetPositionAndRotation(new Vector3(0, 0, 0), Quaternion.Euler(new Vector3(0, 0, 0)));
    15.         this.enabled = true;
    16.         // Customize this to your desired Position(Vector3) & Rotation(Quaternion)
    17.         this.transform.SetPositionAndRotation(new Vector3(0, 5, -5), Quaternion.Euler(new Vector3(0, 0, 0)));
    18.     }
    19. }
     
  19. TarkinStudios

    TarkinStudios

    Joined:
    Jul 31, 2014
    Posts:
    3
    A few hours digging into the documentation, it's really not intuitive enough.

    https://docs.unity3d.com/Packages/c...machineVirtualCameraBase_PreviousStateIsValid

    PreviousStateIsValid
    "Set this to force the next update to ignore deltaTime and reset itself"

    Code (CSharp):
    1. public CinemachineVirtualCamera[] cm;
    2.  
    3.     internal void ImediateTargetLook()
    4.     {
    5.         foreach (var item in cm)
    6.         {
    7.             item.PreviousStateIsValid = false;
    8.         }
    9.     }
     
  20. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,233
    Agreed, the doc is inadequate. Thanks for letting us know.