17
2015
Get Angle Between 2 GameObjects in Degrees (0-360)
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Related Posts
10 Comments + Add Comment
Leave a comment
Recent posts
- MotionVector Effect: Object “disappears” when paused
- [GreaseMonkey] Unity Forum Fixer
- UnityHub: Make Hub application background Translucent
- Customize SpriteShapeRenderer quality (but has issues)
- Editor tool: Copy selected gameobject’s names into clipboard as rows (for Excel)
- Editor tool: Replace string in selected gameobject’s names
- UnityHub: Enable built-in Login Dialog (no more browser login/logout issues!)
- Use TikTok-TTS in Unity (with WebRequest)
- Create Scene Thumbnail Image using OnSceneSaved & OnPreviewGUI
- webgl+javascript TTS
- Using Moonsharp (LUA) + Unity Webgl
- Using 3D gameobject prefabs with Unity Tilemap + NavMesh Surface
Recent Comments
- [Asset Store] Point Cloud Viewer & Tools on
- [Asset Store] Point Cloud Viewer & Tools on
- Vector3 maths for dummies! on
- UnityHub: Make Hub application background Translucent on
- UnityHub: Make Hub application background Translucent on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- [Asset Store] Point Cloud Viewer & Tools on
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?
that is just somewhere from internets,
included in here https://github.com/unitycoder/DoomStyleBillboardTest/tree/master/Assets/Textures
Many thanks 🙂
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