Search Unity

[Solved] How to change cinemachine camera transform

Discussion in 'Cinemachine' started by HuangWM, Jul 9, 2019.

  1. HuangWM

    HuangWM

    Joined:
    Nov 3, 2015
    Posts:
    45
    I create two cinemachine freelook camera, named CMA and CMB. They are follow and lookat Player.
    upload_2019-7-9_20-26-38.png
    First, I enable CMA and disable CMB, then rotate camera
    upload_2019-7-9_20-28-56.png
    Then i disable CMA and enable CMB. At this point I want to set the CMB to the CMA position.
    upload_2019-7-9_20-33-19.png
    I tried this code, but it's invaild. What should I do?
    Code (CSharp):
    1. CMB.transform.position = CMA.transform.position;
    2. CMB.transform.rotation = CMA.transform.rotation;
    3. CMB.m_XAxis.Value = CMA.m_XAxis.Value;
    4. CMB.m_YAxis.Value = CMA.m_YAxis.Value;
     

    Attached Files:

    Last edited: Jul 9, 2019
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    You cannot manually set the transforms of the vcams, as they have procedural algorithms to do that and will overwrite what you set.

    If you are using the most recent version of CM, you should get what you want by enabling "inherit position" in the vcams:

    upload_2019-7-9_9-9-49.png
     
    CacatuaMutante likes this.
  3. HuangWM

    HuangWM

    Joined:
    Nov 3, 2015
    Posts:
    45
    I tried enable Inherit Position, but it didn't solve my problem.
    I upload my test project, it's made with Unity 2018.3.12f1 and Cinemachine 2.2.9.
    Reproduce:
    • Open Scenes/SampleScene
    • Play it
    • Press Q every few seconds
     

    Attached Files:

    Last edited: Jul 12, 2019
  4. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,728
    Thank you for the upload.
    You need to make a few changes to get this to work:
    1. Vcams should not be parented to the main camera with the brain. They can be siblings of a common parent, but they must be independent of each other. You need to correct this in the scene, and in the prefab.
    2. Change the Standby Update in the vcams to "Always"
    3. When the player suddenly changes position because of wrap-around, you need to call vcam.OnTargetObjectWarped() for all vcams that target it, otherwise they will be confused
    I've modified your Test.cs script, to illustrate point #3, and to show you an alternate way to activate the vcams, so that StandbyUpdate == Always will work:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Test : MonoBehaviour
    4. {
    5.     public Cinemachine.CinemachineVirtualCameraBase CM1;
    6.     public Cinemachine.CinemachineVirtualCameraBase CM2;
    7.     public float Speed;
    8.     public float Offset;
    9.  
    10.     private void Start()
    11.     {
    12.         CM1.MoveToTopOfPrioritySubqueue();
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.         transform.position += Vector3.forward * Time.deltaTime * Speed;
    18.         if (transform.position.sqrMagnitude > Offset * Offset)
    19.         {
    20.             var delta = -transform.position;
    21.             transform.position = Vector3.zero;
    22.  
    23.             // Inform any vcams that might be tracking this target
    24.             int numVcams = Cinemachine.CinemachineCore.Instance.VirtualCameraCount;
    25.             for (int i = 0; i < numVcams; ++i)
    26.                 Cinemachine.CinemachineCore.Instance.GetVirtualCamera(i).OnTargetObjectWarped(
    27.                     transform, delta);
    28.         }
    29.  
    30.         if (Input.GetKeyDown(KeyCode.Q))
    31.         {
    32.             if (Cinemachine.CinemachineCore.Instance.IsLive(CM1))
    33.                 CM2.MoveToTopOfPrioritySubqueue();
    34.             else
    35.                 CM1.MoveToTopOfPrioritySubqueue();
    36.         }
    37.     }
    38. }
    Finally, even if you do all these things, you might still notice an occasional 1-frame hiccup when the camera changes. This is actually a bug in CM, which we will fix for the next release.
     
    Destrucity likes this.
  5. HuangWM

    HuangWM

    Joined:
    Nov 3, 2015
    Posts:
    45
    Thanks you, All solved
     
    Gregoryl likes this.