playMaker

Author Topic: Get Signed Angle To Target  (Read 8273 times)

pedroathumanspot

  • Playmaker Newbie
  • *
  • Posts: 11
Get Signed Angle To Target
« on: January 24, 2013, 01:38:50 PM »
Hi!

Here's a custom action I coded thats a variation of the Get Angle To Target action but stores Signed Angle to know if the Target is right (positive) or left (negative) from the GameObject's forward axis:

Code: [Select]
/ * Get Signed Angle to Target - Playmaker Custom Action
  * https://github.com/pedrosanta/PlaymakerActionsPack */

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Transform)]
[Tooltip("Gets the signed Angle (in degrees, clockwise, -180 to 180) between a Game Object's forward axis and a Target. The Target can be defined as a Game Object or a world Position. If you specify both, then the Position will be used as a local offset from the Object's position.")]
public class GetSignedAngleToTarget : FsmStateAction
{
[RequiredField]
public FsmOwnerDefault gameObject;

public FsmGameObject targetObject;

public FsmVector3 targetPosition;

public FsmBool ignoreHeight;

[RequiredField]
[UIHint(UIHint.Variable)]
public FsmFloat storeAngle;

public bool everyFrame;

public override void Reset()
{
gameObject = null;
targetObject = null;
targetPosition = new FsmVector3 { UseVariable = true};
ignoreHeight = true;
storeAngle = null;
everyFrame = false;
}

public override void OnLateUpdate()
{
DoGetAngleToTarget();

if (!everyFrame)
{
Finish();
}
}

void DoGetAngleToTarget()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null)
{
return;
}

var goTarget = targetObject.Value;
if (goTarget == null && targetPosition.IsNone)
{
return;
}

Vector3 targetPos;
if (goTarget != null)
{
targetPos = !targetPosition.IsNone ?
goTarget.transform.TransformPoint(targetPosition.Value) :
goTarget.transform.position;
}
else
{
targetPos = targetPosition.Value;
}

if (ignoreHeight.Value)
{
targetPos.y = go.transform.position.y;
}

var localTarget = go.transform.InverseTransformPoint(targetPos);

// This will only work with vectors on XZ plane, so ignore height is irrelevant. Update description/etc asap.
storeAngle.Value = Mathf.Atan2(localTarget.x, localTarget.z) * Mathf.Rad2Deg;
}

}
}

Additionally I am maintaining a custom actions repository on https://github.com/pedrosanta/PlaymakerActionsPack. Feel free to fork it away and collaborate. :)

Cheers.
« Last Edit: January 20, 2015, 01:21:55 AM by jeanfabre »

Richard_M1

  • Playmaker Newbie
  • *
  • Posts: 2
Re: Get Signed Angle To Target
« Reply #1 on: January 08, 2015, 03:03:10 PM »
Thanks, this works for me and is very useful.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15537
  • Official Playmaker Support
Re: Get Signed Angle To Target
« Reply #2 on: January 20, 2015, 01:22:33 AM »
Hi,
 
Thanks. I noticed the url was wrong for the rep, so I edited your post.

 Bye,

 Jean

crem2002

  • Playmaker Newbie
  • *
  • Posts: 14
Re: Get Signed Angle To Target
« Reply #3 on: July 30, 2015, 07:08:17 AM »
pedroathumanspot you are a life saver. works and just what i needed thanks keep up the good work