- Home /
2D Arrow pointing at 3D Position
This is probably a really stupid, easy to solve problem. It feels like one of these things where you lack a fresh perspective or the programming vocab to pull it off; So here's my Problem:
For my GUI I have a 2D Arrow in 3D Space that is supposed to rotate on its Y-axis to point at a 3D Object. I'm not struggling with the mental model on this, just the execution has escaped me for the past week. Here's the Arrow:
This is the hierarchy its in:
Control being the player's GameObject. Basically this is how I thought to do it: Transpose the position into local space using InverseTransformPoint, then use Math.Atan2 to calculate the angle. That doesn't work and I don't understand why.
tmpVector = arrow.InverseTransformPoint(objectToPointAt.position);
arrow.localEulerAngles = new Vector3(0, Mathf.Atan2(tmpVector.y, tmpVector.z) * Mathf.Rad2Deg, 0);
Thanks in advance for any hints!
The Arrow is sitting in the X-Z Plane. So for the arrow to point, say, to the left side, you'd rotate it 180° on the Y axis. The Camera's view direction changes depending on where the player looks.
The TT is actually a Plane, so is the player. So the TT can be anywhere in space around the Player. So nah, the TT's Y Value is probably never the same as the Arrow's...
Oh gosh yeah, a litteral Aeroplane XD. I feel pretty dumb for not having mentioned that!
Answer by ProudOne · Jul 09, 2012 at 11:02 AM
As I expected, the answer was actually right in Front of me all the time. I needed to add 180 to realign the arrow.
tmpVector = player.transform.InverseTransformPoint(allyIsland.position); // Transfer the point into the local space of the arrow
angleToTarget = Mathf.Atan2(tmpVector.x, tmpVector.z) * Mathf.Rad2Deg; // Calculate the Angle the arrow needs to rotate
angleToTarget += 180.0f; // Add 180 Degrees
guiAllyIslandDir.localEulerAngles = new Vector3(0, angleToTarget, 0); // Rotate!
Thanks, everyone! Here's where I feel really dumb ^^