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.

Thanks in advance for your help!

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.
float distanceToTestFor; //this will be your testing radius
//set the above values however you wish

Ray rayToTest = new Ray( originOfAngle, angle );
Vector3 targetPoint = rayToTest.GetPoint(distanceToTestFor);
//The above line should return your desired Vector3

What this script does: It casts a Ray from a defined point into a defined direction (with a length of infinity). Then, the GetPoint() function will take that ray, “walk up it” at a defined length, and return that point as a Vector3.

Note: test this out, the ray that you cast may end up colliding with other solid objects. If this turns out to be a problem, put the gameObject which this script is attached to on a separate layer that does not collide with anything.

Using a raycast also affords you other niceties, like easy debug renderers and the ability to cast one ray but do multiple things with it, though you may not need all of this functionality.