#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 UnityEngine; [Opsive.Shared.Utility.Category("Animator")] [Opsive.Shared.Utility.Description("Sets animation speed with smooth transitions and optional curve.")] public class SetAnimationSpeed : TargetGameObjectAction { [Tooltip("The target animation speed multiplier.")] [SerializeField] protected SharedVariable m_TargetSpeed = 1.0f; [Tooltip("The transition duration for speed change.")] [SerializeField] protected SharedVariable m_TransitionDuration = 0.5f; [Tooltip("Whether to use smooth transition.")] [SerializeField] protected SharedVariable m_UseSmoothTransition = true; private Animator m_ResolvedAnimator; private float m_StartSpeed; 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; if (m_ResolvedAnimator != null) { m_StartSpeed = m_ResolvedAnimator.speed; } } /// /// Sets the animation speed. /// /// The status of the action. public override TaskStatus OnUpdate() { if (m_ResolvedAnimator == null) { return TaskStatus.Failure; } if (m_UseSmoothTransition.Value && m_TransitionDuration.Value > 0.0f) { m_ElapsedTime += Time.deltaTime; var progress = Mathf.Clamp01(m_ElapsedTime / m_TransitionDuration.Value); var currentSpeed = Mathf.Lerp(m_StartSpeed, m_TargetSpeed.Value, progress); m_ResolvedAnimator.speed = currentSpeed; return progress >= 1.0f ? TaskStatus.Success : TaskStatus.Running; } else { m_ResolvedAnimator.speed = m_TargetSpeed.Value; } return TaskStatus.Success; } /// /// Resets the action values back to their default. /// public override void Reset() { base.Reset(); m_TargetSpeed = 1.0f; m_TransitionDuration = 0.5f; m_UseSmoothTransition = true; } } } #endif