Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Nicoolai · Jul 15, 2019 at 10:04 AM · vectorvector2

How do I translate a vector2 into one of the fixed directions

Hi, Let's say I have some vector2, based on user input.
How do I find the nearest "direction" for that vector?
I would like to get if it's closest to Vector2.left of Vector2.up, as an example.

Is there a trick to this in unity, or do I need to dive into some vector maths?

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

2 Replies

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

Answer by rufopufo · Jul 15, 2019 at 10:52 AM

Hi there,

Firsty, you will need to see to what axis, is your vector nearer. You can achieve this, by simply comparing x and y components of your vector in absolute mode.

Example: Vector (0.3,0.7), has a greater y, so the Y axis, will be the nearest.

Vector (-0.4,0.1), has a greater x in absolute mode, so the X axis, will be the nearest.

Having this, you can handle the direction by observing the sign of your vector component.

Example: (-0.4,0.1), we had determine that X axis is the nearest, and the x component is negative. So Vector2.left will be the desired direction.

(0.3,0.7), we had determine that Y axis was the nearest, and 0.7 is positive, so the direction is upwards.

You don't have to really dive into maths, just compare the components of your vector.

Hope it helps. If you need more help, just comment.

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 Nicoolai · Jul 15, 2019 at 10:58 AM 0
Share

Thanks! It's so simple, I guess I was blinded by my own math-failures.

avatar image rufopufo Nicoolai · Jul 15, 2019 at 11:08 AM 0
Share

Glad I helped :)

avatar image
0

Answer by Ex6tra · May 15 at 01:05 PM

I'll leave this here in case someone comes across this question

     Vector2 Snap(Vector2 dir)
     {
         float absX = Mathf.Abs(dir.x);
         float absY = Mathf.Abs(dir.y);
 
         if (absX > absY)
         {
             return dir.x > 0 ? Vector2.right : Vector2.left;
         }
         else if (absX < absY)
         {
             return dir.y > 0 ? Vector2.up : Vector2.down;
         }
         return Vector2.zero;
     }
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

112 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 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

[HELP] Translating vector 2 co-ordinates to real space 1 Answer

Rb2D.MovePosition not working - weird error... 1 Answer

Vector rotation 1 Answer

Having difficulty with Lerp 3 Answers

Switch force from horizontal to vertical 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