#if GRAPH_DESIGNER /// --------------------------------------------- /// Behavior Designer /// Copyright (c) Opsive. All Rights Reserved. /// https://www.opsive.com /// --------------------------------------------- namespace Opsive.BehaviorDesigner.Runtime.Tasks.Actions.RigidbodyTasks { using Opsive.GraphDesigner.Runtime.Variables; using UnityEngine; [Opsive.Shared.Utility.Category("Rigidbody")] [Opsive.Shared.Utility.Description("Applies impulse with direction, force, and optional relative space.")] public class Impulse : TargetGameObjectAction { [Tooltip("The impulse direction.")] [SerializeField] protected SharedVariable m_Direction = Vector3.forward; [Tooltip("The impulse force magnitude.")] [SerializeField] protected SharedVariable m_Force = 10.0f; [Tooltip("Whether to apply impulse in local space.")] [SerializeField] protected SharedVariable m_UseLocalSpace = false; [Tooltip("Whether the impulse has been applied.")] [SerializeField] [RequireShared] protected SharedVariable m_ImpulseApplied; private UnityEngine.Rigidbody m_ResolvedRigidbody; private bool m_HasApplied; /// /// Called when the state machine is initialized. /// /// /// Initializes the target GameObject. /// protected override void InitializeTarget() { base.InitializeTarget(); m_ResolvedRigidbody = m_ResolvedGameObject.GetComponent(); } /// /// Called when the action starts. /// public override void OnStart() { base.OnStart(); m_HasApplied = false; m_ImpulseApplied.Value = false; } /// /// Updates the impulse application. /// /// The status of the action. public override TaskStatus OnUpdate() { if (m_ResolvedRigidbody == null) { return TaskStatus.Success; } if (!m_HasApplied) { var direction = m_Direction.Value; if (m_UseLocalSpace.Value) { direction = m_ResolvedRigidbody.transform.TransformDirection(direction); } m_ResolvedRigidbody.AddForce(direction.normalized * m_Force.Value, ForceMode.Impulse); m_HasApplied = true; m_ImpulseApplied.Value = true; } return TaskStatus.Success; } /// /// Resets the action values back to their default. /// public override void Reset() { base.Reset(); m_Direction = Vector3.forward; m_Force = 10.0f; m_UseLocalSpace = false; m_ImpulseApplied = null; } } } #endif