- Home /
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!
Answer by robertbu · May 17, 2013 at 11:44 PM
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.
$$anonymous$$athf.Cos() and $$anonymous$$athf.Sin() take radians. If you are using degrees, then you can use $$anonymous$$athf.Deg2Rad.
Answer by animalphase · May 17, 2013 at 11:38 PM
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.
@charlestheoaf - I'm not aware of a constructor for a Ray that takes an angle.
@robertbu - the standard Ray() function does this, as typed out above. I wrote this answer in $$anonymous$$onoDevelop with the help of auto-complete and code hints :)
If you read Ray, the second variable is a direction. A direction is like an angle, but not the same numbers. If the angle is -45 (NorthWest) then the angle
var (which is the direction) could be (-1,0,1)
.
Suppose you had an angle of 30 degrees. What should angle x/y/z be? Not (30,0,0)
, which is just East. The correct direction is (0.5, 0, 0.86)
, which is to the east, but more north. You'd either have to use sin and cos, or Quaternion.Euler(0,30,0)*Vector3.forward
to figure that out.
Thank you for the clarification, Owen. This will actually come in handy for me as well.
I see. 'angle' as used in the code is a Vector3 'direction'. I missed that. The thing is, if he had such a vector, he would not need to create a ray nor do any of the calculations suggested to solve this problem. He could simply do:
Vector3 v3Point = v3direction * distance;
Answer by Owen-Reynolds · May 17, 2013 at 11:56 PM
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 only need x and z coordinates how would i do this with either quaternions or with sin and cos. i would rather use raw algebra because I may need to calculate some of this on the server.
Both give x and z. The Quat method, just look at p2.x
and p2.z
(y is always 0 since you start at y=0 and only spin.) The cos/sin method (other answer) gives the exact same x and z.
Your answer
Follow this Question
Related Questions
Trigonometry Unit Circle Question 1 Answer
Help Understanding Script 2 Answers
Mahtf.inverselerp 3 Answers
How to find inverse cos 1 Answer
Trigonometry help? 1 Answer