Files
2024-12-18 15:25:43 +01:00

80 lines
1.7 KiB
C#

// (c) Copyright HutongGames, LLC 2010-2016. All rights reserved.
using UnityEngine;
namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Physics2D)]
[Tooltip("Gets the 2d Velocity of a Game Object and stores it in a Vector2 Variable or each Axis in a Float Variable. NOTE: The Game Object must have a Rigid Body 2D.")]
public class GetVelocity2d : ComponentAction<Rigidbody2D>
{
[RequiredField]
[CheckForComponent(typeof(Rigidbody2D))]
[Tooltip("The GameObject with the Rigidbody2D attached")]
public FsmOwnerDefault gameObject;
[UIHint(UIHint.Variable)]
[Tooltip("The velocity")]
public FsmVector2 vector;
[UIHint(UIHint.Variable)]
[Tooltip("The x value of the velocity")]
public FsmFloat x;
[UIHint(UIHint.Variable)]
[Tooltip("The y value of the velocity")]
public FsmFloat y;
[Tooltip("The space reference to express the velocity")]
public Space space;
[Tooltip("Repeat every frame.")]
public bool everyFrame;
public override void Reset()
{
gameObject = null;
vector = null;
x = null;
y = null;
space = Space.World;
everyFrame = false;
}
public override void OnEnter()
{
DoGetVelocity();
if (!everyFrame)
Finish();
}
public override void OnUpdate()
{
DoGetVelocity();
}
void DoGetVelocity()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (!UpdateCache(go))
{
return;
}
var velocity = rigidbody2d.linearVelocity;
if (space == Space.Self)
{
velocity = rigidbody2d.transform.InverseTransformDirection(velocity);
}
vector.Value = velocity;
x.Value = velocity.x;
y.Value = velocity.y;
}
}
}