- Home /
[2D] Get 0-359 compass angle player <--> object
The closest I could find on the answerhub was:
float AngleBetweenMouseAndPlayer()
{
Vector3 v3Pos;
float fAngle;
//Convert the player to Screen coordinates
v3Pos = Camera.main.WorldToScreenPoint(transform.position);
v3Pos = Input.mousePosition - v3Pos;
fAngle = Mathf.Atan2(v3Pos.y, v3Pos.x) * Mathf.Rad2Deg;
if (fAngle < 0.0f) fAngle += 360.0f;
return fAngle - 90f;
}
I can rework this code to work for 2 object <--> player. However, in this code the north returns 0, the east -90, the south 180 (the only correct one), and the west 90 degrees...
I need a function that returns me (for 2D space):
0 if the other object is exactly north of the player.
90 if the other object is exactly east of the player.
180 if the other object is exactly south of the player.
270 if the other object is exactly west of the player.
And of course all values in between, but no negative values.
Answer by Hellium · Jun 25, 2015 at 04:24 PM
The following method will give you the signed angle between your vectors (between -180° and 180°)
Simply add 180° to the result
float SignedAngleBetween( Vector3 a, Vector3 b, Vector3 n )
{
float angle = Vector3.Angle( a, b );
float sign = Mathf.Sign( Vector3.Dot( n, Vector3.Cross( a, b ) ) );
return angle * sign;
}
The n
vector is the normal vector to your surface facing up (don't know the value for 2D games, Vector3.up I guess)
See Understanding Vector Arithmetic if you want to understand the mathematic notions involved :
http://docs.unity3d.com/Manual/UnderstandingVectorArithmetic.html
The game-surface is along the XY-axis (topdown view, so the camera is aligned with the Z-axis) if that matters. The Unity default for 2D.
It doesn't work for me. I tried all 5 possible vectors for the normal including the zero. I add +180f to the result but still I get only values between ~180 and ~200. I get no values that make any sense to me. I also tried switching the a&b input parameters but that didn't help either.
I also tried changing Vector3.Angle( a, b ); to Vector2.Angle( a, b ) but it produced the same results (as expected).
The angle is between direction, not points (it's impossible to have an angle between points). If you give position, it's equivalent to the direction (position - (0,0,0)) which gives you a direction.
I think you are right. Perhaps my English isn't so good, my apologies. I changed the title. I guess I need the compass bearing, not the angle. Like if I had a compass and I needed it to point to a specific object (2D space) from the player, how do I find the rotation of the compass in degrees 0-359. I can't find a single topic about this.
Then, keep the function I gave you (just change the vector3 to vector2), but as parameter, pass the following :
// -Vector3.forward or Vector3.forward, don't really know
float angle = SignedAngleBetween( Vector2.up , object.transform.position - player.transform.position, -Vector3.forward )
Thanks a lot, that solved it. I had to add +360 because it went from 0 to $$anonymous$$us 180. Full unrefactored code(attached to the player gameobject):
float SignedAngleBetween(Vector2 a, Vector2 b, Vector3 n) { float angle = Vector2.Angle(a, b); float sign = $$anonymous$$athf.Sign(Vector3.Dot(n, Vector3.Cross(a, b))); return angle * sign; } float AngleTo$$anonymous$$arker() { float angle = SignedAngleBetween(Vector2.up, $$anonymous$$arker.transform.position - transform.position, -Vector3.forward); if (angle < 0) { angle += 360f; } return angle; }
Your answer
Follow this Question
Related Questions
compass GUI jumps at angle 180. 0 Answers
Rotating A Character 180 Degress 1 Answer
By-pass the 'shortest route' aspect of Quaternion.Slerp 1 Answer
0-360 Y degree from Vector3.Angle 1 Answer