#if GRAPH_DESIGNER /// --------------------------------------------- /// Behavior Designer /// Copyright (c) Opsive. All Rights Reserved. /// https://www.opsive.com /// --------------------------------------------- namespace Opsive.BehaviorDesigner.Runtime.Tasks.Actions.AnimatorTasks { using Opsive.GraphDesigner.Runtime.Variables; using Opsive.BehaviorDesigner.Runtime.Tasks.Actions.TransformTasks; using UnityEngine; [Opsive.Shared.Utility.Category("Animator")] [Opsive.Shared.Utility.Description("Animates a parameter value over time with easing curves.")] public class AnimateParameterOverTime : TargetGameObjectAction { /// /// Specifies the parameter type to animate. /// public enum ParameterType { Float, Int } [Tooltip("The type of parameter to animate.")] [SerializeField] protected ParameterType m_ParameterType = ParameterType.Float; [Tooltip("The name of the parameter to animate.")] [SerializeField] protected SharedVariable m_ParameterName; [Tooltip("The starting value.")] [SerializeField] protected SharedVariable m_StartValue = 0.0f; [Tooltip("The target value.")] [SerializeField] protected SharedVariable m_TargetValue = 1.0f; [Tooltip("The duration of the animation.")] [SerializeField] protected SharedVariable m_Duration = 1.0f; [Tooltip("The easing curve type.")] [SerializeField] protected SmoothMoveTo.EasingType m_EasingType = SmoothMoveTo.EasingType.Linear; private Animator m_ResolvedAnimator; private float m_ElapsedTime; /// /// Initializes the target GameObject. /// protected override void InitializeTarget() { base.InitializeTarget(); m_ResolvedAnimator = m_ResolvedGameObject.GetComponent(); } /// /// Called when the action starts. /// public override void OnStart() { base.OnStart(); m_ElapsedTime = 0.0f; } /// /// Animates the parameter value. /// /// The status of the action. public override TaskStatus OnUpdate() { if (m_ResolvedAnimator == null) { return TaskStatus.Failure; } if (string.IsNullOrEmpty(m_ParameterName.Value)) { return TaskStatus.Failure; } m_ElapsedTime += Time.deltaTime; var progress = Mathf.Clamp01(m_ElapsedTime / m_Duration.Value); var easedProgress = ApplyEasing(progress); var currentValue = Mathf.Lerp(m_StartValue.Value, m_TargetValue.Value, easedProgress); if (m_ParameterType == ParameterType.Float) { m_ResolvedAnimator.SetFloat(m_ParameterName.Value, currentValue); } else { m_ResolvedAnimator.SetInteger(m_ParameterName.Value, Mathf.RoundToInt(currentValue)); } if (progress >= 1.0f) { return TaskStatus.Success; } return TaskStatus.Running; } /// /// Applies easing to the value. /// private float ApplyEasing(float t) { switch (m_EasingType) { case SmoothMoveTo.EasingType.EaseIn: return t * t; case SmoothMoveTo.EasingType.EaseOut: return 1.0f - (1.0f - t) * (1.0f - t); case SmoothMoveTo.EasingType.EaseInOut: return t < 0.5f ? 2.0f * t * t : 1.0f - Mathf.Pow(-2.0f * t + 2.0f, 2.0f) / 2.0f; default: return t; } } /// /// Resets the action values back to their default. /// public override void Reset() { base.Reset(); m_ParameterType = ParameterType.Float; m_ParameterName = null; m_StartValue = 0.0f; m_TargetValue = 1.0f; m_Duration = 1.0f; m_EasingType = SmoothMoveTo.EasingType.Linear; } } } #endif