Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by filler03 · May 17, 2013 at 10:17 PM · mathftrigonometryinverse

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image filler03 · May 17, 2013 at 11:52 PM 0
Share

Would i need to use RadtoDeg function for this?

avatar image robertbu · May 18, 2013 at 12:10 AM 1
Share

$$anonymous$$athf.Cos() and $$anonymous$$athf.Sin() take radians. If you are using degrees, then you can use $$anonymous$$athf.Deg2Rad.

avatar image
1

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · May 17, 2013 at 11:47 PM 0
Share

@charlestheoaf - I'm not aware of a constructor for a Ray that takes an angle.

avatar image animalphase · May 18, 2013 at 12:00 AM 0
Share

@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 :)

avatar image Owen-Reynolds · May 18, 2013 at 12:32 AM 2
Share

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.

avatar image animalphase · May 18, 2013 at 12:43 AM 0
Share

Thank you for the clarification, Owen. This will actually come in handy for me as well.

avatar image robertbu · May 18, 2013 at 03:05 AM 0
Share

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;
Show more comments
avatar image
1

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.)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image filler03 · May 18, 2013 at 12:05 AM 0
Share

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.

avatar image Owen-Reynolds · May 18, 2013 at 12:22 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges