#if GRAPH_DESIGNER /// --------------------------------------------- /// Behavior Designer /// Copyright (c) Opsive. All Rights Reserved. /// https://www.opsive.com /// --------------------------------------------- namespace Opsive.BehaviorDesigner.Runtime.Tasks.Actions.CharacterControllerTasks { using Opsive.GraphDesigner.Runtime.Variables; using UnityEngine; [Opsive.Shared.Utility.Category("Character Controller")] [Opsive.Shared.Utility.Description("Enables/disables CharacterController with optional smooth property transition.")] public class EnableDisable : TargetGameObjectAction { [Tooltip("Whether to enable the CharacterController.")] [SerializeField] protected SharedVariable m_Enable = true; [Tooltip("Whether to smoothly transition properties when enabling/disabling.")] [SerializeField] protected SharedVariable m_SmoothTransition = false; [Tooltip("The disabled height (when smooth transition is enabled).")] [SerializeField] protected SharedVariable m_DisabledHeight = 0.1f; [Tooltip("The transition duration (0 = instant).")] [SerializeField] protected SharedVariable m_TransitionDuration = 0.5f; private CharacterController m_ResolvedCharacterController; private float m_OriginalHeight; private float m_ElapsedTime; private bool m_IsTransitioning; /// /// Called when the state machine is initialized. /// /// /// Initializes the target GameObject. /// protected override void InitializeTarget() { base.InitializeTarget(); m_ResolvedCharacterController = m_ResolvedGameObject.GetComponent(); } /// /// Called when the action starts. /// public override void OnStart() { base.OnStart(); m_ElapsedTime = 0.0f; m_IsTransitioning = false; if (m_ResolvedCharacterController != null) { m_OriginalHeight = m_ResolvedCharacterController.height; } } /// /// Updates the enable/disable state. /// /// The status of the action. public override TaskStatus OnUpdate() { if (m_ResolvedCharacterController == null) { return TaskStatus.Success; } if (m_SmoothTransition.Value && m_TransitionDuration.Value > 0.0f) { if (!m_IsTransitioning) { m_IsTransitioning = true; m_ElapsedTime = 0.0f; } m_ElapsedTime += Time.deltaTime; var progress = Mathf.Clamp01(m_ElapsedTime / m_TransitionDuration.Value); if (m_Enable.Value) { m_ResolvedCharacterController.height = Mathf.Lerp(m_DisabledHeight.Value, m_OriginalHeight, progress); if (progress >= 1.0f) { m_ResolvedCharacterController.enabled = true; m_IsTransitioning = false; } } else { m_ResolvedCharacterController.height = Mathf.Lerp(m_OriginalHeight, m_DisabledHeight.Value, progress); if (progress >= 1.0f) { m_ResolvedCharacterController.enabled = false; m_IsTransitioning = false; } } } else { m_ResolvedCharacterController.enabled = m_Enable.Value; } return TaskStatus.Success; } /// /// Resets the action values back to their default. /// public override void Reset() { base.Reset(); m_Enable = true; m_SmoothTransition = false; m_DisabledHeight = 0.1f; m_TransitionDuration = 0.5f; } } } #endif