Cinemachine scripting

Hi,
I’m trying to make a top down camera controller with the help of Cinemachine for a game.
I want to access some of the properties as seen in the inspector.
I’m relatively new to programming and i think i’m just missing something that should be simple.
In the Virtual Camera component there are a few options that I can change, but not all and not the ones i want to adjust in a script.
I found the documentation of the Cinemachine API and found it works with Structs which I’m trying to get my head around and how they differ from classes.
The struct AxisState has some of the values i would like to adjust in my script.

https://docs.unity3d.com/Packages/com.unity.cinemachine@2.1/api/Cinemachine.AxisState.html

Maybe I just need to get a bit more knowledge before diving in, but my main goal atm is to learn.
i just try to modify the behavior of the camera with the middle mouse button for now, but the functionality will be different in the end, but I just don’t understand the syntax for accessing a struct and manipulating the values.

Any help to guide me in the right direction is much appreciated! :slight_smile:

Here is my code so far (with the error i get when i try to run it below), I added this script to the Gameobject that has the virtual camera:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;

public class CameraControlTopDown : MonoBehaviour
{

    AxisState axisState;

    // Start is called before the first frame update
    void Start()
    {
        axisState = GetComponent<AxisState>();

    }

    // Update is called once per frame
    void Update()
    {
      
        if (Input.GetMouseButtonDown(2))
        {
            axisState.m_InputAxisName = "Mouse X";
        }
        if (Input.GetMouseButtonUp(2))
        {
            axisState.m_InputAxisName = null;
        }
    }
}

Error:
ArgumentException: GetComponent requires that the requested component ‘AxisState’ derives from MonoBehaviour or Component or is an interface.
UnityEngine.Component.GetComponentInChildren (System.Type t, System.Boolean includeInactive) (at C:/buildslave/unity/build/Runtime/Export/Scripting/Component.bindings.cs:55)
UnityEngine.Component.GetComponentInChildren[T] () (at C:/buildslave/unity/build/Runtime/Export/Scripting/Component.bindings.cs:72)
CameraControlTopDown.Start () (at Assets/CameraControlTopDown.cs:15)

5417640--550635--part.JPG

1 Like

ArgumentException: GetComponent requires that the requested component 'AxisState' derives from MonoBehaviour or Component or is an interface.

That tells you that the thing you’re trying to get must be MonoBehaviour or Component or an interface. AxisState is none of the above. It is a field within the CinemachineOrbitalTransposer component. So you have to first get the CinemachineOrbitalTransposer component, then access its m_Axis field.

Like this:

using UnityEngine;
using Cinemachine;

public class CameraControlTopDown : MonoBehaviour
{
    CinemachineOrbitalTransposer orbital;

    void Start()
    {
        var vcam = GetComponent<CinemachineVirtualCamera>();
        if (vcam != null)
            orbital = vcam.GetCinemachineComponent<CinemachineOrbitalTransposer>();
    }

    void Update()
    {
        if (orbital != null)
        {
            orbital.m_XAxis.m_InputAxisName = Input.GetMouseButton(2) ? "Mouse X" : "";
        }
    }
}
4 Likes

Thank you very much for the quick reply!
I have been searching for a solution all day and this really makes my day!

1 Like

I’m going crazy with this cinemachine stuff. Nothing is working, send help!
How can i change the target through script??

What target? do you mean the Follow and LookAt properties? they are just Transforms. If you declare an instance of your vCam, you can acceed to all its properties…

Take a look at this how do i change cinemachine follow transform in script? - Unity Forum