- Home /
Angle between 2 GameObjects and center of screen
I have two GameObjects in a scene. I need to project their positions to screen space and then get the angle between them relative to the center of the screen. This stack overflow article has a solution, but, there must be a more efficient way to do this. Any ideas? After projection to screen space getting the lengths of each side of the triangle using Vector2.Distance is easy then I can use the Law of cosines. But it seems a shame to do that if there is a more efficient way.
http://stackoverflow.com/questions/1211212/how-to-calculate-an-angle-from-three-points
Answer by XienDev · Nov 02, 2012 at 02:15 PM
vector3 p = Camera.main.worldToScrean(gameObject1);
vector3 p1 = Camera.main.worldToScrean(gameObject2);
p2 = new Vector2(Screen.Width / 2, Screen.Height / 2);
vector2 sp1 = new Vector2(p.x - p2.x, p.y - p2.y);
vector2 sp2 = new Vector2(p1.x - p2.x, p1.y - p2.y);
float angle = Vector2.Angle(sp1, sp2);
Something like that, wrote first what i took in mind. Here may be an eror; =))
I dont have permission to upvote but I do have permission to mark as answer, says I have to login as another user,wtf, anyway, thanks for the answer.
Answer by valyard · Nov 02, 2012 at 01:45 PM
There's Vector2.Angle which internally must be doing pretty much the same thing. But looks better in a script.