torsdag 25 november 2021

Smooth Zooming with Cinemachine (Unity)

Zooming a Cinemachine Virtual Camera with 3rd Person Follow by changing the camera distance works but there are no transitions between the steps.

There are other ways to zoom that look better.

One way to get it smoother I found out  is to add a extension to your virtual camera called Follow Zoom. It's a script and comes with values you can change in the inspector. The Follow Zoom extension can be added at the bottom of your virtual camera, in the inspector.

You can use the input you use for zoom to alter these values in a script of your own. You use a reference to your virtual camera and do .GetComponent<CinemachineFollowZoom>() to get the Follow Zoom script.

Once you have the Follow Zoom extension you can change the values of these variables to get a desired transition in your zooming:

_cinemachineFollowZoom.m_MinFOV += zoomInput;

_cinemachineFollowZoom.m_MaxFOV += zoomInput;

_cinemachineFollowZoom.m_Width += zoomInput;


ZoomInput above is calculated from the Input Action Asset made in "new" Input System. It's a Vector2, triggered by scrolling and pressing the gamecontroller defined in a created input asset, attached to a Player Input component, added to the player GameObject. Google Unity Input System to learn how to use it. It's not as complicated as it might look.


You will need checks to make sure you stay within the limits set in the inspector.

These conditions aren't allowed:

if (zoomInput > 0 && cineMinFoV >= cineMaxFoV)

if(zoomInput < 0 && cineMinFoV < 1)


I found a value of about 1-4 is pretty good to set "Damping" to in the inspector and Min FOV to 2. Damping needs to be above 0.

måndag 15 november 2021

Unity - How to get access and change the camera distance while using Cinemachine "3rd person follow" (C#)

Cinemachine https://unity.com/unity/features/editor/art-and-design/cinemachine

saves time by letting you create difference kinds of cameras.


Let's say you want to use the input system and scroll the mouse to change the camera distance.

I couldn't find a clear solution online so I thought I share:


First make sure you have:

A player GameObject.

A GameObject with a CinemachineVirtualCamera, set to: 

* follow the player.

* its "Body" set to "3rd Person Follow".


Note: You have other body properties as well to chose from:

3rd Person follow

Framing Transposer

Hard Lock to Target

Orbital Transposer

Tracked Dolly

Transposer


We are focusing on the "3rd Person Follow" now.


In the script that you add as component to the player GameObject:


Declaration: 

private GameObject _playerFollowCamera;

public CinemachineVirtualCamera _cinemachineVirtualCamera;


In private void Start() :

_playerFollowCamera = GameObject.Find("PlayerFollowCamera");

_cinemachineVirtualCamera = _playerFollowCamera.GetComponent<CinemachineVirtualCamera>();


          In your method, called when scrolling for example: 

CinemachineComponentBase cinemachineComponentBase = _cinemachineVirtualCamera.GetCinemachineComponent(CinemachineCore.Stage.Body);


if (cinemachineComponentBase is Cinemachine3rdPersonFollow)

{

(cinemachineComponentBase as Cinemachine3rdPersonFollow).CameraDistance = yourCameraDistanceValue;

}


Or as one line:

(GameObject.Find("PlayerFollowCamera").GetComponent<CinemachineVirtualCamera>().GetCinemachineComponent(CinemachineCore.Stage.Body) 

as Cinemachine3rdPersonFollow).CameraDistance = yourCameraDistanceValue;


"Cinemachine3rdPersonFollow" above is changed depending on which body property you are using.