0
\$\begingroup\$

I'm working on a top-down 3D game where things spawn off camera. Movement only occurs on 2 axis (x and z). Rotation is about y axis.

The player is in the center of the camera and needs to have indicators at the edge of the screen to show where the item is relative to the players location.

I've managed to figure out the distance from my player to the target just using the distance between 2 Vector3 points.

However, I can't figure out how to translate this into a direction/angle for an indicator arrow that I have instantiated as a child of the canvas UI and make it point to the edge of the screen.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

use the arctangent to find the angle

angle = Mathf.Atan2(z2-z1), x2-x1) * Mathf.Rad2Deg;

The output is in Radians, so multiplying by Rad2Deg converts it to degrees for you.

this will result in an angle in the range (-180, 180] degrees. If you need it from (0, 360], then just add:

if (angle < 0) angle += 360;
\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .