- Home /
Deriving and angle from two points
So I need to derive an angle from two points in 2D space.
I have tried this:
Vector2.Angle(p1,p2);
But it doesn't actually give me an angle, it gives a directional vector I think. There must be a simple answer right? my search has turned up allot of confusing information that doesn't seem to directly answer my problem. Perhaps the question is how you'd convert a directional vector to an angle.
Regardless, I'm stuck. Any insight would be appreciated.
Comment
Best Answer
Answer by Poyer · Aug 29, 2011 at 03:10 AM
Alright, this was pretty simple...
to get the angle all i needed was this line:
var angle : float = Mathf.Atan2(p2.y-p1.y, p2.x-p1.x)*180 / Mathf.PI;
A bit nicer with Rad2Deg:
var angle : float = $$anonymous$$athf.Atan2(p2.y-p1.y, p2.x-p1.x) * $$anonymous$$athf.Rad2Deg;
I tried to get lookat to work for a solid hour! You solution turned out to work as perfectly Thanks!