// Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2023 Kybernetik // using UnityEngine; namespace Animancer { /// A which uses . /// /// Documentation: Smoothing /// /// /// [SerializeField] private AnimancerComponent _Animancer; /// [SerializeField] private LinearMixerTransition _Mixer; /// /// private MixerParameterTweenFloat _MixerTween; /// /// private void Awake() /// { /// // Play creates the LinearMixerState from the transition. /// _Animancer.Play(_Mixer); /// /// // Now that the state exists, we can create a tween for it. /// _MixerTween = new MixerParameterTweenFloat(_Mixer.State); /// /// // Start tweening the parameter towards 0.5 over a period of 0.25 seconds. /// _MixerTween.Start(0.5f, 0.25f); /// } /// /// https://kybernetik.com.au/animancer/api/Animancer/MixerParameterTweenFloat /// #if !UNITY_EDITOR [System.Obsolete(Validate.ProOnlyMessage)] #endif public class MixerParameterTweenFloat : MixerParameterTween { public MixerParameterTweenFloat() { } public MixerParameterTweenFloat(MixerState mixer) : base(mixer) { } protected override float CalculateCurrentValue() => Mathf.LerpUnclamped(StartValue, EndValue, Progress); } /************************************************************************************************************************/ /// A which uses . /// See . /// https://kybernetik.com.au/animancer/api/Animancer/MixerParameterTweenVector2 /// #if !UNITY_EDITOR [System.Obsolete(Validate.ProOnlyMessage)] #endif public class MixerParameterTweenVector2 : MixerParameterTween { public MixerParameterTweenVector2() { } public MixerParameterTweenVector2(MixerState mixer) : base(mixer) { } protected override Vector2 CalculateCurrentValue() => Vector2.LerpUnclamped(StartValue, EndValue, Progress); } /************************************************************************************************************************/ /// A system which interpolates a over time. /// See . /// https://kybernetik.com.au/animancer/api/Animancer/MixerParameterTween_1 /// #if !UNITY_EDITOR [System.Obsolete(Validate.ProOnlyMessage)] #endif public abstract class MixerParameterTween : Key, IUpdatable { /************************************************************************************************************************/ /// The target . public MixerState Mixer { get; set; } /************************************************************************************************************************/ /// The value of the when this tween started. public TParameter StartValue { get; set; } /// The target value this tween is moving the towards. public TParameter EndValue { get; set; } /************************************************************************************************************************/ /// The amount of time this tween will take (in seconds). public float Duration { get; set; } /// The amount of time that has passed since the (in seconds). public float Time { get; set; } /// The normalized progress (0 to 1) of this tween towards its goal. public float Progress { get => Time / Duration; set => Time = value * Duration; } /************************************************************************************************************************/ /// Creates a new . public MixerParameterTween() { } /// Creates a new and sets the . public MixerParameterTween(MixerState mixer) => Mixer = mixer; /************************************************************************************************************************/ /// /// Sets the details of this tween and registers it to be updated so that it can apply its effects every frame. /// public void Start(TParameter endValue, float duration) { #if UNITY_ASSERTIONS AnimancerUtilities.Assert(Mixer != null, nameof(Mixer) + " is null."); AnimancerUtilities.Assert(Mixer.Root != null, $"{nameof(Mixer)}.{nameof(Mixer.Root)} is null."); #endif StartValue = Mixer.Parameter; EndValue = endValue; Duration = duration; Time = 0; Mixer.Root.RequirePreUpdate(this); } /************************************************************************************************************************/ /// Stops this tween from updating. public void Stop() => Mixer?.Root?.CancelPreUpdate(this); /************************************************************************************************************************/ /// Is this tween currently being updated? public bool IsActive => IsInList(this); /************************************************************************************************************************/ /// /// Called every update while this tween is active to calculate the what value to set the /// to. Usually based on the , /// , and . /// protected abstract TParameter CalculateCurrentValue(); /************************************************************************************************************************/ void IUpdatable.Update() { Time += AnimancerPlayable.DeltaTime; if (Time < Duration)// Tween. { Mixer.Parameter = CalculateCurrentValue(); } else// End. { Time = Duration; Mixer.Parameter = EndValue; Stop(); } } /************************************************************************************************************************/ } }