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.
There’s Vector2.Angle which internally must be doing pretty much the same thing. But looks better in a script.
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; =))