#if GRAPH_DESIGNER /// --------------------------------------------- /// Behavior Designer /// Copyright (c) Opsive. All Rights Reserved. /// https://www.opsive.com /// --------------------------------------------- namespace Opsive.BehaviorDesigner.Runtime.Tasks.Actions.PhysicsTasks { using Opsive.GraphDesigner.Runtime.Variables; using UnityEngine; [Opsive.Shared.Utility.Category("Physics")] [Opsive.Shared.Utility.Description("Applies force to Rigidbody with mode selection and optional relative direction.")] public class ApplyForceWithMode : TargetGameObjectAction { /// /// Specifies the force mode. /// public enum ForceModeType { Force, Impulse, Acceleration, VelocityChange } [Tooltip("The force vector to apply.")] [SerializeField] protected SharedVariable m_Force; [Tooltip("The force mode to use.")] [SerializeField] protected ForceModeType m_ForceMode = ForceModeType.Force; [Tooltip("Whether to apply the force in local space relative to the Rigidbody.")] [SerializeField] protected SharedVariable m_UseLocalSpace = false; [Tooltip("Whether to apply force at a specific position.")] [SerializeField] protected SharedVariable m_ApplyAtPosition = false; [Tooltip("The position to apply force at. Only used if Apply At Position is true.")] [SerializeField] protected SharedVariable m_Position; private Rigidbody m_ResolvedRigidbody; /// /// Initializes the target GameObject. /// protected override void InitializeTarget() { base.InitializeTarget(); m_ResolvedRigidbody = m_ResolvedGameObject.GetComponent(); } /// /// Applies the force to the Rigidbody. /// /// The status of the action. public override TaskStatus OnUpdate() { if (m_ResolvedRigidbody == null) { return TaskStatus.Success; } var force = m_Force.Value; if (m_UseLocalSpace.Value) { force = m_ResolvedRigidbody.transform.TransformDirection(force); } var forceMode = (ForceMode)(int)m_ForceMode; if (m_ApplyAtPosition.Value) { m_ResolvedRigidbody.AddForceAtPosition(force, m_Position.Value, forceMode); } else { m_ResolvedRigidbody.AddForce(force, forceMode); } return TaskStatus.Success; } /// /// Resets the action values back to their default. /// public override void Reset() { base.Reset(); m_Force = null; m_ForceMode = ForceModeType.Force; m_UseLocalSpace = false; m_ApplyAtPosition = false; m_Position = null; } } } #endif