#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("Sets velocity with constraint checking and smooth interpolation.")] public class SetVelocityWithConstraints : TargetGameObjectAction { [Tooltip("The target velocity.")] [SerializeField] protected SharedVariable m_TargetVelocity; [Tooltip("The interpolation speed (0 = instant).")] [SerializeField] protected SharedVariable m_InterpolationSpeed = 0.0f; [Tooltip("Whether to respect position constraints.")] [SerializeField] protected SharedVariable m_RespectPositionConstraints = true; [Tooltip("Whether to respect rotation constraints.")] [SerializeField] protected SharedVariable m_RespectRotationConstraints = true; private UnityEngine.Rigidbody m_ResolvedRigidbody; /// /// Called when the state machine is initialized. /// /// /// Initializes the target GameObject. /// protected override void InitializeTarget() { base.InitializeTarget(); m_ResolvedRigidbody = m_ResolvedGameObject.GetComponent(); } /// /// Updates the Rigidbody velocity. /// /// The status of the action. public override TaskStatus OnUpdate() { if (m_ResolvedRigidbody == null) { return TaskStatus.Success; } var targetVelocity = m_TargetVelocity.Value; if (m_RespectPositionConstraints.Value) { var constraints = m_ResolvedRigidbody.constraints; if ((constraints & RigidbodyConstraints.FreezePositionX) != 0) { targetVelocity.x = 0.0f; } if ((constraints & RigidbodyConstraints.FreezePositionY) != 0) { targetVelocity.y = 0.0f; } if ((constraints & RigidbodyConstraints.FreezePositionZ) != 0) { targetVelocity.z = 0.0f; } } #if UNITY_6000_3_OR_NEWER if (m_InterpolationSpeed.Value > 0.0f) { m_ResolvedRigidbody.linearVelocity = Vector3.Lerp(m_ResolvedRigidbody.linearVelocity, targetVelocity, m_InterpolationSpeed.Value * Time.deltaTime); } else { m_ResolvedRigidbody.linearVelocity = targetVelocity; } #else if (m_InterpolationSpeed.Value > 0.0f) { m_ResolvedRigidbody.velocity = Vector3.Lerp(m_ResolvedRigidbody.velocity, targetVelocity, m_InterpolationSpeed.Value * Time.deltaTime); } else { m_ResolvedRigidbody.velocity = targetVelocity; } #endif return TaskStatus.Success; } /// /// Resets the action values back to their default. /// public override void Reset() { base.Reset(); m_TargetVelocity = null; m_InterpolationSpeed = 0.0f; m_RespectPositionConstraints = true; m_RespectRotationConstraints = true; } } } #endif