- Home /
How to get 0-360 degree from two points
Is there a function that will return degrees (0-360) from one point (x = 0, z = 0) to another point (-x = 3, -z = 5)
Edited to add exact code snippet that worked for me.
GameObject target = GameObject.Find("Name Of Game Object In Hierarchy");
float MyPositionX = transform.position.x;
float MyPositionZ = transform.position.z;
float TargetPositionX = target.transform.position.x;
float TargetPositionZ = target.transform.position.z;
degree = FindDegree(MyPositionX - TargetPositionX, MyPositionZ - TargetPositionZ);
public static float FindDegree(float x, float y)
{
float value = (float)((System.Math.Atan2(x, y) / System.Math.PI) * 180f);
if (value < 0) value += 360f;
return value;
}
You can use Vector3.Angle and make it relative to some given world axis.
You'll also need to correct for the case where the angle is >180, since Vector3.Angle returns the acute angle.
Answer by YoungDeveloper · Aug 20, 2015 at 07:42 PM
This will take classic x and y
private void Start(){
Debug.Log(FindDegree(0, 1));
}
public static float FindDegree(int x, int y){
float value = (float)((Mathf.Atan2(x, y) / Math.PI) * 180f);
if(value < 0) value += 360f;
return value;
}
If you want to use this from transform positions, then vector3.angle is what you are looking for. As you have to know what is forward to calculate it, i believe there even a ready example if you google.
btw: $$anonymous$$athf does also have a PI constant as float, so that casting from double to float wouldn't be necessary. There are also two conversion constants:
Those are factors so you just have to multiply an angle in radians with "$$anonymous$$athf.Rad2Deg" to get degrees.
Also keep in $$anonymous$$d that the parameters of the Atan2 method are swapped. The parameters are y and x. The example on the docs page is actually wrong ^^. They are swapped because it actually calculates the arctangent of "y / x" and in addition takes the quadrant into account by looking at the signs of x and y. It also handles the case of "x == 0" to prevent a division by 0. See wikipedia as well.
@Bunny83 I actually use my own $$anonymous$$ath lib, where i have my own constant PI. It was actually a typo from my side using $$anonymous$$athf.Atan2 i meant $$anonymous$$ath in this example. As i share my libs in various c# projects i never use unity $$anonymous$$athf lib, as its pretty much a wrapper around System.$$anonymous$$ath. For example here's a source for $$anonymous$$athf.Atan2().
public static float Atan2(float y, float x){
return (float)$$anonymous$$ath.Atan2((double)y, (double)x);
}
So we have ton of casts here, what i do in my libs is usually calculate evetyhing and only then cast once. But even that down cast is zero overhead, as its pretty much a number cut down, not even a round.
What comes to the function i posted, i know about atan swap, but if I swap Atan2 correctly , just like docs say, it actually produces mirrored results for x axis. I did tests with these examples, and results are perfect. ;)
Debug.Log(FindDegree(0, 0)); //0 - 360
Debug.Log(FindDegree(0, 1)); //0 - 360
Debug.Log(FindDegree(1, 1)); //45
Debug.Log(FindDegree(1, 0)); //90
Debug.Log(FindDegree(0, -1)); //180
Debug.Log(FindDegree(-1, -1)); //225
Debug.Log(FindDegree(-1, 0)); //270
Debug.Log(FindDegree(-1, 1)); //315
@YoungDeveloper: I know that Unity's $$anonymous$$athf class is just a wrapper for System's $$anonymous$$ath class. I didn't ment the cast overhead but the cast just makes it harder to read ^^. That's actually the reason why Unity provided that wrapper class for $$anonymous$$ath since Untiy only works with floats ins$$anonymous$$d of doubles.
If you use your own $$anonymous$$ath lib (or System's one) you should do:
public static float FindDegree(double x, double y){
double value = ($$anonymous$$ath.Atan2(x, y) / $$anonymous$$ath.PI) * 180;
if(value < 0d) value += 360d;
return (float)value;
}
to $$anonymous$$imize casting and precision problems. Since your x and y are casted to double anyways you can simply define your parameters as double. ints and floats are implicitly casted to double.
$$anonymous$$irrored results? In mathematics the unit circle has 0° on the right and goes counter clockwise around the circle.
x, y
-----------
1, 0 -> 0°
1, 1 -> 45°
0, 1 -> 90°
-1, 1 -> 135°
-1, 0 -> 180° // or -180°
-1,-1 -> 225° // or -135°
0,-1 -> 270° // or -90°
1,-1 -> 315° // or -45°
The given angle should result in this vector:
Vector2(Cos(angle), Sin(angle))
Which is the "opposite" operation of Atan2.
$$anonymous$$eep in $$anonymous$$d this is a pure mathematics function and definition. In the context of Unity everything is a bit different since in mathematics you usually work with a right-handed system while Unity uses a left-handed system. Likewise as you might know there are many different diffinitions for Euler angles and Unity only uses one of them ^^.
@Bunny83 yeah thats exactly what i do in my libs, i tried to write it fast unity style for the op lol.
Sorry to dig up an old thread, but this just wont work for me...
I have the exact same FindDegree method, but when giving it the values 0 and 25, it returns 0 as the angle? Has something changes since to make this method not work any more?
That's actually correct, if you drew a line from 0 strait up 25 units, the degree to that point from 0 would be 0 degrees or 360 degrees.
Answer by eppz · Feb 15, 2017 at 12:39 PM
I made an extension, use like this:
float angle = transform.position.AngleTo(touch.position);
Just put this into a CS file somewhere:
public static class _Vector2
{
public static float AngleTo(this Vector2 this_, Vector2 to)
{
Vector2 direction = to - this_;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
if (angle < 0f) angle += 360f;
return angle;
}
}
This is not (!) mirrored, as Atan2 takes y coordinate first.
Your answer
Follow this Question
Related Questions
Can't get Math Functions to Work in Compute Shader 0 Answers
ForceMode.Acceleration estimated dst != covered dst with a single Addforce in effect 1 Answer
Programmatically specifying a random point in an arc in front of the player 1 Answer
Circular motion via the mathematical circle equation? 4 Answers
Events called every Nth day 1 Answer