playMaker

Author Topic: SetDistance2D - Calculates new position based on angle and distance.  (Read 1815 times)

centaurianmudpig

  • Playmaker Newbie
  • *
  • Posts: 10
Perhaps a better name for the action can be given.  Anyhow, here is the action.

Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions {
    [ActionCategory("Vector2")]
    [Tooltip("Calulates the 2D position from a given angle and length.")]
    public class SetDistance2D : FsmStateAction
    {
        [UIHint(UIHint.Variable)]
        [Tooltip("Required angle (theta) in degrees")]
        public FsmFloat theta;
        [Tooltip("Distance to move by")]
        public FsmFloat distance;
        [Tooltip("Store distance as a Vector2")]
        public FsmVector2 fsmStoreVector2;
        public FsmFloat fsmStoreX;
        public FsmFloat fsmStoreY;

        public override void Reset()
        {
            theta = null;
            distance = null;
        }

        public override void OnEnter()
    {
            float rads = theta.Value * Mathf.PI / 180;
            Vector2 newVector = new Vector2(
                Mathf.Cos(rads) * distance.Value,
                -Mathf.Sin(rads) * distance.Value
                );

            if(fsmStoreVector2.UsesVariable) fsmStoreVector2.Value = newVector;
            if (fsmStoreX.UsesVariable) fsmStoreX.Value = newVector.x;
            if (fsmStoreY.UsesVariable) fsmStoreY.Value = newVector.y;

            Finish();
    }
    }
}