Search Unity

How to change look at target for CineMachine?

Discussion in 'Getting Started' started by CyberInteractiveLLC, Jul 13, 2018.

  1. CyberInteractiveLLC

    CyberInteractiveLLC

    Joined:
    May 23, 2017
    Posts:
    307
    So i finally am happy with my cinemachine setup, however... I want to set the target by script, I've tried it this way but unsuccessful

    Code (CSharp):
    1.     Cinemachine.CinemachineVirtualCamera c_VirtualCam;
    2.  
    3.     [SerializeField] Transform target;
    4.  
    5.     private void Awake()
    6.     {
    7.         c_VirtualCam = this.GetComponent<Cinemachine.CinemachineVirtualCamera>();
    8.     }
    9.     private void Start()
    10.     {
    11.         c_VirtualCam.LookAt(target);
    12.     }

    EDIT: This worked!!

    Code (CSharp):
    1.     Cinemachine.CinemachineVirtualCamera c_VirtualCamera;
    2.     [SerializeField] Transform target;
    3.  
    4.     private void Awake()
    5.     {
    6.         c_VirtualCamera = GetComponent<Cinemachine.CinemachineVirtualCamera>();
    7.     }
    8.     private void Start()
    9.     {
    10.         c_VirtualCamera.m_LookAt = target.transform;
    11.         c_VirtualCamera.m_Follow = target.transform;
    12.     }
     
    Last edited: Jul 13, 2018
  2. noneyabiznus

    noneyabiznus

    Joined:
    Oct 17, 2021
    Posts:
    7
    this doesn't work for me :/
     
  3. noneyabiznus

    noneyabiznus

    Joined:
    Oct 17, 2021
    Posts:
    7
    NullReferenceException: Object reference not set to an instance of an object
     
  4. vdhiraj

    vdhiraj

    Joined:
    Jul 13, 2019
    Posts:
    2
    it worked thank you
     
  5. jbnlwilliams1

    jbnlwilliams1

    Joined:
    May 21, 2019
    Posts:
    267
    Did you set your target in the inspector?
     
  6. stevanlaw

    stevanlaw

    Joined:
    Feb 22, 2022
    Posts:
    1
    Thank you so much.
    It is working..:cool:;)
     
  7. deny250

    deny250

    Joined:
    Nov 27, 2020
    Posts:
    1
    roses are red, violets are blue, this code is now copyed and pasted.
     
  8. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    1,026
    I hate to be pedantic but copying and pasting code doesn't always work.

    I did it on a project myself last week, it didn't work.

    Also, if you type in the code yourself, watch your spelling/grammar, it's VERY sensitive.