Checked (after a coffee-break) a couple other actions to see how they did their thing... I think I cracked this one... So, have at it. (I have to still finish this up by polishing it up but I tested it and this does work now.
using UnityEngine;
using System.Collections;
namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Character)]
[Tooltip("Simple movement of a character while constrained within a user-defined area.")]
public class ClampedMovement : FsmStateAction
{
private Rigidbody rb;
public enum AxisPlane
{
XZ,
XY,
YZ
}
[RequiredField]
[CheckForComponent(typeof(Rigidbody))]
[Tooltip("The GameObject to move.")]
public FsmOwnerDefault gameObject;
[RequiredField]
[Tooltip("The movement vector.")]
public FsmVector3 moveVector;
[RequiredField]
[Tooltip("The world plane that the 2d input is mapped onto.")]
public AxisPlane mappedToPlane;
[Tooltip("Lateral clamp minimum value.")]
public FsmFloat horizontalMin;
[Tooltip("Lateral clamp maximum value.")]
public FsmFloat horizontalMax;
[Tooltip("Vertical clamp minimum value.")]
public FsmFloat verticalMin;
[Tooltip("Vertical clamp maximum value.")]
public FsmFloat verticalMax;
public override void Reset()
{
gameObject = null;
moveVector = null;
mappedToPlane = AxisPlane.XZ;
horizontalMax = 0.0f;
horizontalMin = 0.0f;
verticalMax = 0.0f;
verticalMin = 0.0f;
}
public override void OnFixedUpdate()
{
DoSetVelocity();
}
void DoSetVelocity()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);
rb = go.GetComponent<Rigidbody>();
rb.velocity = moveVector.Value;
switch (mappedToPlane)
{
case AxisPlane.XZ:
go.transform.position = new Vector3 (Mathf.Clamp(go.transform.position.x, horizontalMin.Value, horizontalMax.Value), go.transform.position.y, Mathf.Clamp(go.transform.position.z, verticalMin.Value, verticalMax.Value));
break;
case AxisPlane.XY:
go.transform.position = new Vector3 (Mathf.Clamp(go.transform.position.x, horizontalMin.Value, horizontalMax.Value), Mathf.Clamp(go.transform.position.y, verticalMin.Value, verticalMax.Value), go.transform.position.z);
break;
case AxisPlane.YZ:
go.transform.position = new Vector3 (go.transform.position.y, Mathf.Clamp(go.transform.position.y, horizontalMin.Value, horizontalMax.Value), Mathf.Clamp(go.transform.position.z, verticalMin.Value, verticalMax.Value));
break;
}
}
}
}
I Think I'm going to have to keep in mind this kinda stuff though (and I have no idea if this is the most elegant way of doing it since this is my first time working with custom actions and also seriously working with C# as is.) But for the most part this is a start and possibly a final action.
I know I'll likely have to add in some redundancy and error-checking since I suspect if the values for the borders of the clamping are not filled in it'll cause errors... But at least it's a start.