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 Kaemalux · Sep 27, 2013 at 02:37 PM · movementrigidbodydiagonal

How to prevent diagonal movement on 2d-rigidbody script?

Hello!

I have this script, attached to a simple cube player, and it is working well, detecting collision and objects (thanks to rigid body). Problem is that i cannot manage to let him move "just" one direction.. I mean, if i press another key while moving, the movement is diagonal, instead i would like to have him stopping one direction and only enabling the new one.

If someone can help me, i would really appreciate!

     #pragma strict
     
     //how fast the player walks
     var walkSpeed:float = 14.0;
     
     
     function Update () {
     
     if(Input.GetKey("a") || Input.GetKey("d") || Input.GetKey("left") || Input.GetKey("right"))
     {
     
         if(Input.GetKey("a") || Input.GetKey("left"))
         {
             if(rigidbody.velocity.x > 0)
             {
                 rigidbody.velocity.x = 0;
             }
             if(rigidbody.velocity.x > -walkSpeed)
             {
             rigidbody.velocity.x -= 48*Time.deltaTime;
             }
         }    
  
         if(Input.GetKey("d")|| Input.GetKey("right"))
         {
             if(rigidbody.velocity.x < 0)
             {
                 rigidbody.velocity.x = 0;
             }
         if(rigidbody.velocity.x < walkSpeed)
             {
             rigidbody.velocity.x += 48*Time.deltaTime;
             }
         }
     
     }
     else    
     {
     
         rigidbody.velocity.x = 0.0;
     }
     if(Input.GetKey("w") || Input.GetKey("s") || Input.GetKey("up") || Input.GetKey("down"))
     {
         
         if(Input.GetKey("s") || Input.GetKey("down"))
         {
             if(rigidbody.velocity.z > 0)
             {
                 rigidbody.velocity.z = 0;
             }
             if(rigidbody.velocity.z > -walkSpeed)
             {
                 rigidbody.velocity.z -= 48*Time.deltaTime;
             }
         }
 
         if(Input.GetKey("w")|| Input.GetKey("up"))
         {
             if(rigidbody.velocity.z < 0)
             {
                 rigidbody.velocity.z = 0;
             }
         if(rigidbody.velocity.z < walkSpeed)
             {
             rigidbody.velocity.z += 48*Time.deltaTime;
             }
         }
     
     }
     else
     {
     
         rigidbody.velocity.z = 0.0;
     
     
     }
     
 }

Ps: i'm learning Unity and C#, though i found the script among a js compilation... If someone could help me translate this also, it would be a really nice favor!

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
0
Best Answer

Answer by Fattie · Sep 27, 2013 at 03:55 PM

Just click the button in the inspector ..

look at the rigidbody in the Inspector in the editor.

Comment
Add comment · Show 5 · 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 Kaemalux · Sep 27, 2013 at 04:06 PM 0
Share

Which button? Sorry, i'm quite newbie, cannot catch this... Is there a simple solution? Thanks

avatar image Fattie · Sep 27, 2013 at 04:12 PM 0
Share

Look at unity, look at inspector, look at rigidbody, look at CONSTRAINTS.

awesome eh !

avatar image Kaemalux · Sep 27, 2013 at 04:22 PM 0
Share

Yeah, awesome! I understood that i should (maybe?) mark all rotations + position.x/position.z depending on what it is happening. But i cannot get well that bitwise thing, considering, that rotations are all locked by default, have i to write some code like this?

 if (Get$$anonymous$$eyDown("a") || Get$$anonymous$$eyDown("d"))
 rigidbody.constraints = RigidbodyConstraints.FreezePositionY;
 if (rigid body.velocity.x == 0)
 rigidbody.constraints = RigidbodyConstraints.None;

is it correct? Thanks again Fattie!

avatar image Kaemalux · Sep 27, 2013 at 04:57 PM 0
Share

Thanks i solved! I got what you were referring to!

avatar image Kaemalux · Sep 27, 2013 at 04:58 PM 0
Share

I got the process you were referring to, thanks a lot, i managed to have a nice movement controller!

avatar image
0

Answer by robertbu · Sep 27, 2013 at 02:40 PM

You can put lines 42 - 67 just below line 40 in the 'else' clause. This will eliminate diagonal movement, but it gives dominance to horizontal movement. That is if a vertical and a horizontal key are pressed, your character will move horizontally.

It is fairly complex to recode this so that the first key (horizontal or vertical) is what is done. You can switch the logic and embed the horizontal in the else clause of the vertical if you wanted to give vertical preference.

Comment
Add comment · Show 3 · 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 Kaemalux · Sep 27, 2013 at 02:55 PM 0
Share

Thanks for your help, it is surely a starting. I tried different solutions (i'm a newbie, so i went on without too much knowledge!) as using a boolean value, repeating rigid body.velocity lines, but nothing... Do you have any suggestion to achieve that result? A method i could try myself to set up, or you think is too complex? Thanks again!

avatar image robertbu · Sep 27, 2013 at 04:34 PM 0
Share

I did not read your question/code carefully enough. In addition to the changing the key code, you will need to zero out the movement on the other axes. For example, when processing a vertical key (z), set velocity.x = 0;

avatar image Kaemalux · Sep 28, 2013 at 07:49 AM 0
Share

Thanks however robertbu, i appreciated a lot your help even if i used Fattie method!

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

17 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

Related Questions

4 Direction Ball Movement 1 Answer

Disable diagonal movement in 3D 1 Answer

Touch movement not working 1 Answer

How to let a GameObject generate force but not be affected by certain forces? 0 Answers

Rigidbody climbing stairs 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