Search Unity

Controller for cinemachine easier to add?

Discussion in 'Cinemachine' started by DageLV, Mar 6, 2020.

  1. DageLV

    DageLV

    Joined:
    Feb 5, 2018
    Posts:
    17
    hi.
    Anyone knows a version of cinemachine or something which is just more readable?
    I need a insanely simple system, i press Q, camera moves up, fov gradually increases to 70 and tracked object offset values change a tiny bit. i press E, the reverse happens.

    I managed to backtrack where the camera location is, but then i tried to backtrack fov and there is just some
    public LensSettings m_Lens = LensSettings.Default;
    that doesnt work, i cant change fov value by using
    LensSettings.FieldOfView
    because unity throws back errors like
    CinemachineVirtualCamera.cs(47,5): error CS0120: An object reference is required for the non-static field, method, or property 'LensSettings.FieldOfView'
    i cant change fov from the LensSettings.cs script as well etc.
    Anyone knows?
     
  2. marc_tanenbaum

    marc_tanenbaum

    Unity Technologies

    Joined:
    Oct 22, 2014
    Posts:
    637
    This is super easy to do with a Cinemachine Mixing Camera. Just create one from Cinemachine > Create Mixing Camera and set the two child Virtual Cameras to the FOVs you want (and make any offset changes you require also). You can then control the mix with a simple script like this one:
    Code (CSharp):
    1. using Cinemachine;
    2. using UnityEngine;
    3.  
    4. public class CamControl : MonoBehaviour
    5. {
    6.     private CinemachineMixingCamera mixer;
    7.  
    8.     public float transitionTIme = 2f;
    9.  
    10.     private float currentTransitionTime = 0;
    11.     private float weightDestination = 0;
    12.     private float currentWeight = 0;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         currentTransitionTime = 0;
    18.         weightDestination = 0;
    19.         currentWeight = 0;
    20.         mixer = GetComponent<CinemachineMixingCamera>();
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.         if (Input.GetKeyDown(KeyCode.Q))
    27.         {
    28.             currentTransitionTime = 0;
    29.             weightDestination = 1;
    30.         } else if (Input.GetKeyDown(KeyCode.E))
    31.         {
    32.             currentTransitionTime = 0;
    33.             weightDestination = 0;
    34.         }
    35.  
    36.         if (currentWeight != weightDestination)
    37.         {
    38.             currentTransitionTime += Time.deltaTime / transitionTIme;
    39.             currentWeight = Mathf.Lerp(currentWeight, weightDestination, currentTransitionTime);
    40.          
    41.             // This is really the only Cinemachine-specific code here
    42.             mixer.SetWeight(0, currentWeight);
    43.             mixer.SetWeight(1, 1-currentWeight);
    44.         }
    45.     }
    46. }
    If you ignore the code for getting the key inputs and the lerping, there are really only two lines of Cinemachine code in there (three if you count the one where I cache the reference to the mixer itself.
     
    Last edited: Mar 6, 2020