Files
Hasebe/Library/PackageCache/com.unity.cinemachine@1b8d58983403/Samples~/Shared Assets/Scripts/MixCamerasBasedOnSpeed.cs
T
2026-09-24 20:49:06 +03:00

43 lines
1.3 KiB
C#

using UnityEngine;
namespace Unity.Cinemachine.Samples
{
[RequireComponent(typeof(CinemachineMixingCamera))]
public class MixCamerasBasedOnSpeed : MonoBehaviour
{
[Tooltip("At and above this speed, the second camera is going to be in control completely.")]
public float MaxSpeed;
public Rigidbody Rigidbody;
CinemachineMixingCamera m_Mixer;
float m_CurrentWeight;
void Start()
{
m_Mixer = GetComponent<CinemachineMixingCamera>();
}
void OnValidate()
{
MaxSpeed = Mathf.Max(1, MaxSpeed);
}
void Update()
{
if (Rigidbody == null)
return;
#if UNITY_2023_3_OR_NEWER
var t = Mathf.Clamp01(Rigidbody.linearVelocity.magnitude / MaxSpeed);
#else
var t = Mathf.Clamp01(Rigidbody.velocity.magnitude / MaxSpeed);
#endif
// Weights are not smoothed by cinemachine. If the weights come from a non-smooth source
// (in this case physics), it is important to smooth them to avoid jitter
m_CurrentWeight = Mathf.Lerp(m_CurrentWeight, t, Time.deltaTime);
m_Mixer.Weight0 = 1 - m_CurrentWeight;
m_Mixer.Weight1 = m_CurrentWeight;
}
}
}