98 lines
2.0 KiB
C#
98 lines
2.0 KiB
C#
// (c) Copyright HutongGames, LLC 2010-2016. All rights reserved.
|
|
|
|
using UnityEngine;
|
|
|
|
namespace HutongGames.PlayMaker.Actions
|
|
{
|
|
[ActionCategory(ActionCategory.Physics2D)]
|
|
[Tooltip("Sets the 2d Velocity of a Game Object. To leave any axis unchanged, set variable to 'None'. NOTE: Game object must have a rigidbody 2D.")]
|
|
public class SetVelocity2d : ComponentAction<Rigidbody2D>
|
|
{
|
|
[RequiredField]
|
|
[CheckForComponent(typeof(Rigidbody2D))]
|
|
[Tooltip("The GameObject with the Rigidbody2D attached")]
|
|
public FsmOwnerDefault gameObject;
|
|
|
|
[Tooltip("A Vector2 value for the velocity")]
|
|
public FsmVector2 vector;
|
|
|
|
[Tooltip("The y value of the velocity. Overrides 'Vector' x value if set")]
|
|
public FsmFloat x;
|
|
|
|
[Tooltip("The y value of the velocity. Overrides 'Vector' y value if set")]
|
|
public FsmFloat y;
|
|
|
|
[Tooltip("Repeat every frame.")]
|
|
public bool everyFrame;
|
|
|
|
public override void Reset()
|
|
{
|
|
gameObject = null;
|
|
vector = null;
|
|
// default axis to variable dropdown with None selected.
|
|
x = new FsmFloat { UseVariable = true };
|
|
y = new FsmFloat { UseVariable = true };
|
|
|
|
|
|
everyFrame = false;
|
|
}
|
|
|
|
public override void Awake()
|
|
{
|
|
Fsm.HandleFixedUpdate = true;
|
|
}
|
|
|
|
// TODO: test this works in OnEnter!
|
|
public override void OnEnter()
|
|
{
|
|
DoSetVelocity();
|
|
|
|
if (!everyFrame)
|
|
{
|
|
Finish();
|
|
}
|
|
}
|
|
|
|
public override void OnFixedUpdate()
|
|
{
|
|
DoSetVelocity();
|
|
|
|
if (!everyFrame)
|
|
{
|
|
Finish();
|
|
}
|
|
}
|
|
|
|
void DoSetVelocity()
|
|
{
|
|
var go = Fsm.GetOwnerDefaultTarget(gameObject);
|
|
if (!UpdateCache(go))
|
|
{
|
|
return;
|
|
}
|
|
|
|
// init position
|
|
|
|
Vector2 velocity;
|
|
|
|
if (vector.IsNone)
|
|
{
|
|
velocity = rigidbody2d.velocity;
|
|
|
|
}
|
|
else
|
|
{
|
|
velocity = vector.Value;
|
|
}
|
|
|
|
// override any axis
|
|
|
|
if (!x.IsNone) velocity.x = x.Value;
|
|
if (!y.IsNone) velocity.y = y.Value;
|
|
|
|
// apply
|
|
|
|
rigidbody2d.velocity = velocity;
|
|
}
|
|
}
|
|
} |