I’m trying to implement a smooth change of Follow Offset in Cinemachine. I was able to make it change, but I don’t know how to implement smoothness.
I have an object on which hangs a script with vector3 and a script for changing the camera on the player.
public CameraSettings _CameraSettings;
public GameObject _Camera;
private void OnTriggerEnter(Collider other)
{
var _vcam = _Camera.GetComponent<CinemachineFollow>();
_CameraSettings = other.GetComponent<CameraSettings>();
if (other.gameObject.tag == "CameraInput")
{
Debug.Log(_CameraSettings.CameraVector);
_vcam.FollowOffset = _CameraSettings.CameraVector;
}
}
The easiest way to smoothly transition between different camera settings is to implement a second CinemachineCamera for your trigger region, and activate it allowing the CinemachineBrain to make the blend. When you deactivate it, the brain will blend back.
You can use the CinemachineTriggerAction script to acivate/deactivate cameras, or boost their priority.
Otherwise, you’ll have to write code to gradually change the setting over a given time.
I don’t need a transition between cameras. I just need to smoothly change the follow offset of the camera, but I don’t know how to implement it.
If you really want to do it via code, you can use a coroutine.
Here is how to change ANY quantity smoothly:
Smoothing the change between any two particular values:
You want two variables:
currentLane
desiredLane
When you process user input, you set desiredLane to what it should be.
Every Update() you use Mathf.MoveTowards() (or Vector.MoveTowards() ) to move currentLane gradually to desiredLane.
You have currentQuantity and desiredQuantity.
only set desiredQuantity
the code always moves currentQuantity towards desiredQuantity
read currentQuantity for the smoothed value
Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.
The code: SmoothMovement.cs · GitHub
Another approach would be to use a tweening package such as LeanTween, DOTween or iTween.
OR… just make a simple canned animation… No code needed!