Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Sendatsu_Yoshimitsu · Oct 01, 2016 at 07:00 AM · vector2coordinatesconversiondegrees

How do I convert 2d polar coordinates to a Vector2?

I'm working on a top-down movement script, I have the desired heading as a float in absolute degrees (0.0f should always go towards the top of the screen, 180.0f should go straight down), and likewise I have the desired speed as a float.

Is there anything in the API that would translate my heading in degrees to a Vector2, so I could apply both settings by calling something like RigidBody2.AddForce(headingAsVector * speed)?

Comment
Add comment · Show 6
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 Sendatsu_Yoshimitsu · Oct 01, 2016 at 10:40 AM 0
Share

Hmm, some screwing around with trigonometry produced what I thought was a good solution:

   Vector2 DegreeToVector2(float degree)
   {
       return new Vector2($$anonymous$$athf.Cos(degree), $$anonymous$$athf.Sin(degree));
   }

However, I think I messed something up. because this produces really weird results: 0.0f moves straight right, and 270.0f moves in nearly the identical direction. Am I missing a conversion somewhere that would establish the top of the screen as 0, or is my trig just totally off?

avatar image Bonfire-Boy Sendatsu_Yoshimitsu · Oct 01, 2016 at 01:08 PM 0
Share

The trig functions use radians, so that should be $$anonymous$$athf.Cos(degree * $$anonymous$$athf.Deg2Rad) etc.

avatar image Sendatsu_Yoshimitsu Bonfire-Boy · Oct 01, 2016 at 01:27 PM 0
Share

Hmm, that gets rid of the weird behavior, but it's orienting in a strange way: I'm trying to align my degree measurements to a compass rose, so 0 is always due north (straight up on the screen), 90 is east (right), 180 is south, and 270 is west. Using radians, it's offset: 0 degrees creates a vector that moves due east (right), 90 is north, 180 is west, and 270 is south. Is there an additional step I'm missing to make sure the conversion occurs relative to the y axis?

Show more comments
Show more comments

2 Replies

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

Answer by Bonfire-Boy · Oct 01, 2016 at 03:33 PM

You were close.

First, the formula you used is for when the angle is measured anti-clockwise from the positive direction of the x axis. For your way of measuring the angle, you'd need to swap the trig functions over.

Second, the trig functions need the angle in radians.

I always like my function names to be fully descriptive so I don't accidentally misuse them. So I've also renamed it...

 Vector2 DegreeToUnitVector2(float degrees)
 {
      float radians = degrees * Mathf.Deg2Rad; 
      return new Vector2(Mathf.Sin(radians), Mathf.Cos(radians));
 }
 
Comment
Add comment · 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
0

Answer by Vyra · Oct 01, 2016 at 04:28 PM

Try the following:

 void Update() {
     // Clamp it to prevent any potential errors
     heading = Mathf.Clamp(heading, 0f, 180f);

     // Offset it by 90 degrees and invert it to go from 0d...180d heading to 90d...-90d angle
     headingRadians = (heading - 90f) * -1 * Mathf.Deg2Rad; 

     headingToVector = new Vector2 (Mathf.Cos (headingRadians), 
         Mathf.Sin (headingRadians));
     myRigidBody2D.AddForce (headingToVector * speed);
 }
Comment
Add comment · 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

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

56 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

world to screen coordinates conversion 1 Answer

Convert screenspace coordinates (top-left origin) to worldspace coordinates. Read before you react! 1 Answer

Convert Canvas Space point to local space of a rotated rect? 2 Answers

Find cardinal direction of collision based on 2D Cartesian coordinates 1 Answer

Converting direction to Vector2 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