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 Noxide7 · Jun 18, 2013 at 06:51 PM · keyboardcontrols

How do I make forward and backward speed not affect each other?

I'm new to C# and Unity in general so please excuse me for not knowing most of the terms. I'm having trouble getting my players movement control to work right. My forward speed is faster than my back speed so when I press both keys at once, the character will still move forward only a bit slower. I want to be able to press forward and still have the up and down buttons to work the same but not allow the back button to work until I let go of the forward button... and vice-versa.

 public float forwardSpeed = 7;
     public float backSpeed = 4;
     public float dropSpeed = 4;
     public float riseSpeed = 5;
 
 void Update () {
         if(Input.GetKey("up")) {
               transform.position = transform.position + (transform.up * riseSpeed * Time.deltaTime);
            }
            if(Input.GetKey("down")) {
               transform.position = transform.position - (transform.up * dropSpeed * Time.deltaTime);
            }
            if(Input.GetKey("left")) {
               transform.position = transform.position - (transform.right * backSpeed * Time.deltaTime);
            }
            if(Input.GetKey("right")) {
               transform.position = transform.position + (transform.right * forwardSpeed * Time.deltaTime);
            }
     }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Deathcalibur · Jun 18, 2013 at 07:43 PM

One simple solution is to check if "competing" keys are pushed down in your if statements. For example:

 if(Input.GetKey("up")) ... //stays the same

 if(Input.GetKey("down") && !Input.GetKey("up"))... //! is logically NOT whatever

In this case, up will always have precedence over the down key. It's not the best solution necessarily but something to get you started with. You can do the same with left and right if you need. There are better ways to handle this if you want the first/last key pressed to take precedence.

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

15 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

Related Questions

Javascript coding on how to zoom in using keyboard 1 Answer

same keyboard multiplayer for two cars 0 Answers

Basic Keycontrole doesnt work 2 Answers

Best Practice Multiple Control Methods 1 Answer

Removing Joystick Controls 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