# Finding coordinates based on an angle and distance?

I have a script that determines an angle based on the current transform.position variables.

angle = Mathf.Atan2(transform.position.x, transform.position.z) * Mathf.Rad2Deg;

I now need an inverse for this equation that will allow me to determine the x and z coordinates based on an angle and distance from the origin. I am a bit stumped.

You can use polar to rectangular conversion equation. If ‘r’ is the distance to the point, then:

``````x = r * cos(angle);
y = r * sin(angle);
``````

There are other ways to solve this problem, especially if you are trying to solve it for 3D space instead of 2D. For example you could rotate a vector using an angle/axis rotation.

A more “Unityish” approach is to use Quaternions. Make the angle as a Quat, then shoot a length D arrow that way. Assume you have a y spin (but this works for any 3D spins) of Angle and want to move Dist:

``````Vector3 p2 = Quaternion.Euler(0,Angle,0) * (Vector3.forward * Dist);
``````

This uses the Unity system of 0 degrees is +Z, moving clock-wise (cos and sin require radians where 0 is +X, and CCW. The conversion is easy, but surprisingly error-prone for me.)

I think an easy solution is to cast a ray and use Ray.GetPoint:

``````Vector3 originOfAngle; //this will be where you are casing your angle from.
Vector3 angle; //this will be your target angle.