Enrico Cortinovis
Enrico Cortinovis

Reputation: 861

Change Camera Distance of Cinemachine in script

In my (2D) game, I am making a function that zooms out the camera, and I'm using cinemachine. Is there a way to do it in the script? I looked at the documentation and everywhere but didn't find anything. Only how to change the field of view (that can't work for me).

![enter image description here]1

Thanks in advance!

Upvotes: 6

Views: 16044

Answers (2)

CodinRonin
CodinRonin

Reputation: 195

After stumbling on a similar issue you had, i found out you can do this :

CinemachineComponentBase componentBase = virtualCamera.GetCinemachineComponent(CinemachineCore.Stage.Body);
if (componentBase is CinemachineFramingTransposer)
{
    (componentBase as CinemachineFramingTransposer).m_CameraDistance = 30; // your value
}

Upvotes: 9

Spencer Stream
Spencer Stream

Reputation: 616

What I would suggest is creating a new virtual camera with your desired distance and position and disabling its GameObject to begin. Then, from a script, you can change the current camera to the new camera by disabling the current camera and enabling the new camera. Cinemachine will automatically handle the transition to the new camera.

Here is a method I use to manage multiple cameras in this way.

public GameObject[] Cameras;

public void ActivateCamera(int index)
{
    for (int i = 0; i < Cameras.Length; i++)
    {
        if (i == index)
        {
            Cameras[i].SetActive(true);
        }
        else
        {
            Cameras[i].SetActive(false);
        }
    }
}

Upvotes: 1

Related Questions