Dec
17
2015

Get Angle Between 2 GameObjects in Degrees (0-360)

get_angle_between_2_gameobjects_unity

Small helper script to check angle between 2 objects in degrees (and in between 0-360).

It also includes test code for atan2Approximation, have not measured if there are any benefits using it..
Also note [ExecuteInEditMode], so it runs in editor without playmode.


using UnityEngine;
using System.Collections;
// helper script for testing angle calculation
// USAGE:
// – Attach this script to objectA and assign objectB as target
// – Then select objectA and move it around ObjectB and you can see angle values in inspector
[ExecuteInEditMode]
public class GetAngle : MonoBehaviour
{
public Transform target;
public float angle;
public float angleOpt;
void Update ()
{
if (!target) return;
var myPos = transform.position;
myPos.y = 0;
var targetPos = target.position;
targetPos.y = 0;
Vector3 toOther = (myPos – targetPos).normalized;
angle = Mathf.Atan2(toOther.z, toOther.x) * Mathf.Rad2Deg + 180;
angleOpt = atan2Approximation(toOther.z, toOther.x) * Mathf.Rad2Deg + 180;
Debug.DrawLine (myPos, targetPos, Color.yellow);
}
float atan2Approximation(float y, float x) // http://http.developer.nvidia.com/Cg/atan2.html
{
float t0, t1, t2, t3, t4;
t3 = Mathf.Abs(x);
t1 = Mathf.Abs(y);
t0 = Mathf.Max(t3, t1);
t1 = Mathf.Min(t3, t1);
t3 = 1f / t0;
t3 = t1 * t3;
t4 = t3 * t3;
t0 = – 0.013480470f;
t0 = t0 * t4 + 0.057477314f;
t0 = t0 * t4 – 0.121239071f;
t0 = t0 * t4 + 0.195635925f;
t0 = t0 * t4 – 0.332994597f;
t0 = t0 * t4 + 0.999995630f;
t3 = t0 * t3;
t3 = (Mathf.Abs(y) > Mathf.Abs(x)) ? 1.570796327f-t3 : t3;
t3 = (x < 0) ? 3.141592654f-t3:t3;
t3 = (y < 0) ? -t3 : t3;
return t3;
}
}

view raw

GetAngle.cs

hosted with ❤ by GitHub

get_angle_between_2_gameobjects_unity_ss


Related Posts

About the Author:

.fi

10 Comments + Add Comment

  • Very nice. Many thanks for sharing this very useful script.
    Offtopic: Could you pls be so kind and share the test texture with numbers/grid on it with us as well?

  • This is one of the many examples of very basic things that should be included by Unity out of the box. Way too often we have to reinvent the wheel.

    Regarding the optimization of the atan2 you should take a look here – it was enlightening for me: http://www.performancesimulations.com/wp/how-to-get-big-speed-increases-in-unitys-physics-or-any-math-heavy-code/

  • Yes, thank you. I am working out an angle issue in my current project. Question for you though, what controls where the angle starts/is measured from. A cube with no rotation in a scene has it’s transform.forward pointing straight up. But this script has that at orientation to another transform as 90 degrees? Is there a way to change that? Thanks again.

  • Many Thanks!

  • Thanks for sharing this man! Works like a charm!

  • If you need the script to find the angle on all 3 axis, just change it to what I have below. Note that I did not need his Atan2Approximation part, so I removed it. I also removed his valueless return statement in the update function as I have been told that having a return statement without a value attached is bad coding technique.
    ————————————————-

    using System.Collections;
    using UnityEngine;

    [ExecuteInEditMode]
    public class FindAngleDifference : MonoBehaviour {
    public Transform target;
    public Vector3 angle;

    private void Start()
    {
    angle = Vector3.zero;
    }

    void Update () {
    if(target != null)
    {
    Vector3 myPos = transform.position;
    Vector3 targetPos = target.position;

    Vector3 toOther = (myPos – targetPos).normalized;
    angle.x = Mathf.Atan2(toOther.z, toOther.y) * Mathf.Rad2Deg + 180;
    angle.y = Mathf.Atan2(toOther.z, toOther.x) * Mathf.Rad2Deg + 180;
    angle.z = Mathf.Atan2(toOther.y, toOther.x) * Mathf.Rad2Deg + 180;

    Debug.DrawLine(myPos, targetPos, Color.yellow);
    }
    }
    }

  • Ignore my code. I just discovered that it actually doesn’t work. It appears to work because the line is drawn correct, but the angle it gives is wrong.

  • very nice bro lots of thanks

Leave a comment

Connect

Twitter View LinkedIn profile Youtube Youtube Join Discord Twitch Instagram

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.