#if GRAPH_DESIGNER /// --------------------------------------------- /// Behavior Designer /// Copyright (c) Opsive. All Rights Reserved. /// https://www.opsive.com /// --------------------------------------------- namespace Opsive.BehaviorDesigner.Runtime.Tasks.Actions.Physics2DTasks { using Opsive.GraphDesigner.Runtime.Variables; using UnityEngine; [Opsive.Shared.Utility.Category("Physics2D")] [Opsive.Shared.Utility.Description("Sets Rigidbody2D velocity with smooth interpolation and optional constraints.")] public class SetVelocity2D : 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 apply velocity in local space.")] [SerializeField] protected SharedVariable m_UseLocalSpace = false; [Tooltip("Whether to preserve X velocity.")] [SerializeField] protected SharedVariable m_PreserveX = false; [Tooltip("Whether to preserve Y velocity.")] [SerializeField] protected SharedVariable m_PreserveY = false; private Rigidbody2D m_ResolvedRigidbody2D; /// /// Called when the action starts. /// /// /// Initializes the target GameObject. /// protected override void InitializeTarget() { base.InitializeTarget(); m_ResolvedRigidbody2D = m_ResolvedGameObject.GetComponent(); } /// /// Updates the Rigidbody2D velocity. /// /// The status of the action. public override TaskStatus OnUpdate() { if (m_ResolvedRigidbody2D == null) { return TaskStatus.Success; } Vector2 targetVelocity; if (m_UseLocalSpace.Value) { var angle = m_ResolvedRigidbody2D.rotation * Mathf.Deg2Rad; var cos = Mathf.Cos(angle); var sin = Mathf.Sin(angle); var targetVelocityValue = m_TargetVelocity.Value; targetVelocity = new Vector2(targetVelocityValue.x * cos - targetVelocityValue.y * sin, targetVelocityValue.x * sin + targetVelocityValue.y * cos); } else { targetVelocity = m_TargetVelocity.Value; } #if UNITY_6000_3_OR_NEWER var currentVelocity = m_ResolvedRigidbody2D.linearVelocity; #else var currentVelocity = m_ResolvedRigidbody2D.velocity; #endif var newVelocity = m_InterpolationSpeed.Value > 0.0f ? Vector2.Lerp(currentVelocity, targetVelocity, m_InterpolationSpeed.Value * Time.deltaTime) : targetVelocity; if (m_PreserveX.Value) { newVelocity.x = currentVelocity.x; } if (m_PreserveY.Value) { newVelocity.y = currentVelocity.y; } #if UNITY_6000_3_OR_NEWER m_ResolvedRigidbody2D.linearVelocity = newVelocity; #else m_ResolvedRigidbody2D.velocity = newVelocity; #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_UseLocalSpace = false; m_PreserveX = false; m_PreserveY = false; } } } #endif