3

I'm working on Unity/C# now and I'm stuck with accessing CinemachineVirtualCamera's camera distance value in the script. What I'm trying to do is change the value of camera distance in the body section. enter image description here

First of all, how can I access the CinemachineVirtualCamera component in this game object? The MoveScript is what I attached to a player game object, and I want to zoom out the camera depending on the player's movement. Since the game I'm making is small, I won't make other .cs files.

I wrote

public class MoveScript: MonoBehaviour
{
    private GameObject camObj;
    void Start()
    {
        camObj = GameObject.Find("Vertical Follow Camera");
        camObj.GetComponent<CinemachineVirtualCamera>(); // <- but I get error saying, The type or namespace name 'CinemachineVirtualCamera' could not be found 

    }
}

I also read this document and I think the m_CameraDistance is what I'm looking for but how can I access that value?

Yuuu
  • 715
  • 1
  • 9
  • 32

3 Answers3

3

For anyone else who wondering

GetComponent<CinemachineVirtualCamera>()
 .GetCinemachineComponent<CinemachineFramingTransposer>()
  .m_CameraDistance
HuySora
  • 31
  • 1
  • 4
0

As stated in the linked document, these classes are in Cinemachine namespace. To access the classes you have to either add

using Cinemachine;

to the beginning of your script or change your script to

public class MoveScript: MonoBehaviour
{
    private GameObject camObj;
    void Start()
    {
        camObj = GameObject.Find("Vertical Follow Camera");
        camObj.GetComponent<Cinemachine.CinemachineVirtualCamera>();
    }
}

And as for accessing the m_CameraDistance variable, HuySora already answered that part

dawidsk12345
  • 67
  • 1
  • 8
-1

Try this and don't forgot to mention namespace

public class MoveScript: MonoBehaviour
{
  private CinemachineVirtualCamera virtualCamera;
  private GameObject camObj;
  void Start()
  {
      camObj = GameObject.Find("Vertical Follow Camera");
      virtualCamera = camObj.GetComponent<CinemachineVirtualCamera>();
      float f = virtualCamera.m_CameraDistance;

   }
 }
Jaimin
  • 501
  • 4
  • 13