Playmaker Forum

PlayMaker Updates & Downloads => Share New Actions => Topic started by: iloveaphextwin on August 18, 2013, 08:47:52 AM

Title: XY Angle between Two Points
Post by: iloveaphextwin on August 18, 2013, 08:47:52 AM
Hi there,

I was going crazy trying to calculate the angle between two game objects using only x and y. So I made a simple action that works like most 2d programs. Math.atan to get the angle. Works perfectly for me so hope it can help someone.

Code: [Select]
using UnityEngine;
using HutongGames.PlayMaker;

[ActionCategory(ActionCategory.Transform)]
[Tooltip("XY angle")]

public class xyAngleBetweenTwoPoints : FsmStateAction {
[RequiredField]
[Tooltip("The game object whose forward axis we measure from. If the target is dead ahead the angle will be 0.")]
public FsmOwnerDefault gameObject;

[Tooltip("The target object to measure the angle to. Or use target position.")]
public FsmGameObject targetObject;

[RequiredField]
[UIHint(UIHint.Variable)]
[Tooltip("Store the angle in a float variable.")]
public FsmFloat storeAngle;

[Tooltip("Repeat every frame.")]
public bool everyFrame;

public override void Reset() {
gameObject = null;
targetObject = null;
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) {
return;
}

float xDiff = goTarget.transform.position.x - go.transform.position.x;
float yDiff = goTarget.transform.position.y - go.transform.position.y;
storeAngle.Value =  Mathf.Atan2(yDiff, xDiff) * (180 / Mathf.PI);

}

}

Thanks

Will
Title: Re: XY Angle between Two Points
Post by: jeanfabre on August 19, 2013, 03:33:16 AM
Hi,

 Also, I made a package on the wiki to work with angles. You may find in there some useful actions as well.

https://hutonggames.fogbugz.com/default.asp?W971

bye,

Jean