Search Unity

How to change aim from script?

Discussion in 'Cinemachine' started by unity_PJQFq87ckSe_qA, May 16, 2019.

  1. unity_PJQFq87ckSe_qA

    unity_PJQFq87ckSe_qA

    Joined:
    Dec 22, 2017
    Posts:
    4
    How do I assign new values or access existing values of the freelook camera rigs from script? I've included an image with the variables I'd like to change.

    Edit: I've managed to add an offset by using CinemachineCameraOffset. Although I've created the behavior that I needed I'd still appreciate an answer on how to access those variables.
     

    Attached Files:

    Last edited: May 16, 2019
  2. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    vcam.GetCinemachineComonent<CinemachineComposer>().m_ScreenX = bla
     
    mjamie and unity_PJQFq87ckSe_qA like this.
  3. unity_PJQFq87ckSe_qA

    unity_PJQFq87ckSe_qA

    Joined:
    Dec 22, 2017
    Posts:
    4
  4. Dulop57

    Dulop57

    Joined:
    Dec 9, 2016
    Posts:
    1
    What am I doing wrong? when I press the button it crashes and shows this:


    NullReferenceException: Object reference not set to an instance of an object
    cameraController.Update () (at Assets/cameraController.cs:20)


    This is the script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Cinemachine;
    5.  
    6. public class cameraController : MonoBehaviour
    7. {
    8.     public CinemachineVirtualCamera vcam;
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.      
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         if(Input.GetButton("Move"))
    19.             {
    20.             vcam.GetCinemachineComponent<CinemachineComposer>().m_ScreenX = 100f;
    21.             }
    22.         }
    23. }
    24.  
    The script is in the CM FreeLook1 object.

    Thank you for the help
     
  5. Gregoryl

    Gregoryl

    Unity Technologies

    Joined:
    Dec 22, 2016
    Posts:
    7,730
    Your vcam member is never initialized. It is null. Initialize it in Start().
     
    Dulop57 likes this.