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 GustavRakeberg · Feb 07, 2017 at 07:23 PM · directionif-statementsvector2determine

Determine the direction of a gameobject from vector2 without using if/else?

I have a gameobject in a 2D game that is controlled by a vector2 for the direction it is moving. the values in that vector2 come from input.GetAxis("Horizontal") etc. I need to determine what direction it is moving in from that vector2 (up/ down/ Left/ Right). i could easily use a few if/else statements to figure it out, but it feels like there is a mush easier and more efficient way of doing it. Is there anyone who know of another way or know if if-statements are the way to go? Thanks in advance.

Comment
Add comment · Show 8
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 tanoshimi · Feb 07, 2017 at 07:24 PM 0
Share

Huh? The Vector2 is the direction it's moving in, surely?

avatar image GustavRakeberg tanoshimi · Feb 07, 2017 at 07:44 PM 0
Share
 transform.position += direction * speed;

That is how i am moving the gameobject, so yes, that is the direction. The direction is the vector2. it can have a value of -1,0,1 on both x and y. Example: (1,0) will move it straight up.

avatar image tanoshimi GustavRakeberg · Feb 07, 2017 at 07:54 PM 0
Share

Right..... so if you agree that you already know the direction, what's your question? You want to translate that into the words "up", "diagonal up-left", "right" etc.?

Show more comments
Show more comments
avatar image hexagonius · Feb 07, 2017 at 10:16 PM 1
Share

what states do you want to get into? just might be wrong, but a 2d blendtree could work in your case

avatar image GustavRakeberg hexagonius · Feb 08, 2017 at 08:43 AM 0
Share

i have some states like idle, walking, paused etc, but when i'm in the walking state i have sub states (up, down, left, right). i have the vector2 that technically tells me the direction, but i want to direct the path the code is taking depending on the direction the vector2 is representing. I could use if statements to check what direction it has and then check if the direction has changed since last update, but i feel like there is a better way. I googled blendtree really fast, and that might be something that could work. will look in to that.

avatar image GustavRakeberg hexagonius · Feb 08, 2017 at 09:21 AM 0
Share

Just looked at some totorials on blendtree, and that will work. thank you.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ifdef_ben · Feb 07, 2017 at 10:43 PM

Well if your are moving your character on the x-z plane then you can most likely simply do:

 Vector3 direction3D = new Vector3();
 direction3D.x = direction2D.x;
 direction3D.z = direction2D.y;
 transform.position += direction3D * speed;
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 GustavRakeberg · Feb 08, 2017 at 08:54 AM 0
Share

That is basically exactly what i have done, but that is not what i need help with. I want another script to know what direction the gameobject is moving. i have come so far that i have basically the same vector2 in that other script, but i want deter$$anonymous$$e the path the code should take depending on the direction that vector2 is representing. As I mentioned earlier, i could check it with if-statements every update, but i feel like there is a better way to do it.

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

6 People are following this question.

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

Related Questions

Why doesn't my object move left? 1 Answer

Determine global movement of gameObject 2 Answers

Swipe not 100% perfect 0 Answers

Weird angle returned 1 Answer

Top-Down Enemy Ai Animation 0 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