• 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 r3po · Oct 15, 2010 at 04:25 AM · anglevectormagnitude

Does unity implement a way to convert an angle+magnitude representation of a vector to an x,y vector?

Hi all,

I need to create a two dimensional vector (z is 0) out of an angle and a magnitude. I can do this myself using trigonometric functions but it would be better if Unity already had something I could use.

Any ideas?

Thanks in advance.

R3po

Comment
Add comment · Show 1
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 Jesse Anders · Oct 15, 2010 at 04:49 AM 0
Share

I don't recall there being such a function, but it should be straightforward to create it yourself if needed (as you noted).

1 Reply

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

Answer by skovacs1 · Oct 15, 2010 at 03:38 PM

There are lots of ways to achieve this, but it would be at least two-three+ function calls because you would need to:

  • convert the angle into a direction
  • multiply by the magnitude

But the order doesn't really matter, and then you'd have to convert to a vector2 if you need it that way.

The real problem in writing a function like that is that there is no way to know what the angle is relative to and so you must assume:

//Gets an XY direction of magnitude from a degree angle relative to the x axis
//Rotates a vector of magnitude along the x axis by degrees about the z axis
function GetXYDirection(angle : float, magnitude : float) : Vector2 {
    var rotation : Quaternion = Quaternion.AngleAxis(angle,Vector3.forward);
    var XYZdirection : Vector3 = rotation * Vector3(magnitude,0.0f,0.0f);
    return Vector2(XYZdirection);
}

But implementing your own with trigonometric functions is potentially more run-time efficient, depending on how you write it because either way it will involve at least one cosine and one sine, but doing it yourself in 2D would save you 1 whole dimension of multiplication.

//Gets an XY direction of magnitude from a radian angle relative to the x axis
//Simple version
function GetXYDirection(angle : float, magnitude : float) : Vector2 {
    return = Vector2(Mathf.Cos(angle), Mathf.Sin(angle)) * magnitude;
}
Comment
Add comment · Show 1 · 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 r3po · Oct 16, 2010 at 06:22 PM 0
Share

Exactly what I was looking for! Thank you! I didn't know you could multiply a vector by a quaternion, thanks for that too!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Vector to angle 3 Answers

Arbitrary rotation around a point works except when up flips? 1 Answer

Facing angle of an object 1 Answer

Calculate launch angle based on launch direction 2 Answers

Rotate so VectorY points to another Vector3 2 Answers


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