• 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 boonda · Oct 30, 2014 at 02:42 PM · movementobjectcontrols

coding object movement help

Hello, I'm trying to code an object that moves UP/DOWN/LEFT/RIGHT on a flat surface, no diagonal. I'm using a basic if statement like this:

 if(Input.GetKey("a"))
 {
 //moveLeft
 }
 else
 if(Input.GetKey("w"))
 {
 //moveUp
 }
 else
 if(Input.GetKey("d"))
 {
 //moveRight
 }
 else
 if(Input.GetKey("s"))
 {
 //moveDown
 }

The problem is that I want the order in which the keys are pressed to not matter. Right now, if I press W(up) while holding A(left), the object ignores W and keeps moving left because it's the first in the if statement. On the other hand, if I press D(right) while holding S(down), the direction changes and the object moves right. If I let go of D(right), the object goes back to moving down; This last bit is how I want all the keys to react. How do I fix this in code? Any help would be great, thanks!

Comment
Add comment · Show 2
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 SaraCecilia · Oct 30, 2014 at 10:12 AM 0
Share

Could you provide us with the code of how you're moving the object? Is it a rigid body using physics...or?

avatar image boonda · Oct 30, 2014 at 10:25 AM 0
Share

no physics, no rigid body...just an object with a code like what I posted that moves it in 4 directions

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by okiwanben0bi · Oct 30, 2014 at 03:23 PM

 private float moveLeftRight=0;
 private float moveUpDown=0;

in Update:

 // GetKeyDOWN gets the key once when it is pressed. So, you ever get the "newest" key.
 if(Input.GetKeyDown("a"))
 {
   moveLeftRight= - 1.0f;
   moveUpDown=0.0f;
 }
 
 if(Input.GetKeyDown("d"))
 {
   moveLeftRight= 1.0f;
   moveUpDown=0.0f;
 }
 
 if(Input.GetKeyDown("w"))
 {
   moveLeftRight=0.0f;
   moveUpDown= - 1.0f;
 }
 
 if(Input.GetKeyDown("s"))
 {
   moveLeftRight=0.0f;
   moveUpDown= 1.0f;
 }

 // Maybe reset the values.

 // GetKeyUP gets the key once when it is released.
 if(Input.GetKeyUp("a") && moveLeftRight== - 1.0f)
 {
   moveLeftRight=0.0f;
 }

 if(Input.GetKeyUp("d") && moveLeftRight== 1.0f)
 {
   moveLeftRight=0.0f;
 }

 if(Input.GetKeyUp("w") && moveUpDown== - 1.0f)
 {
   moveUpDown=0.0f;
 }

 if(Input.GetKeyUp("s") && moveUpDown== 1.0f)
 {
   moveUpDown=0.0f;
 }

 // (NO if)
 //move left or right (X += speed * moveLeftRight);
 //move up or down (Y += speed * moveUpDown);



I hope that helps. Not tested.

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 boonda · Oct 31, 2014 at 10:11 AM 0
Share

Thanks. This gives me some ideas and I'll experiment further, but it doesn't do the whole thing that I want. If I'm holding down W and then press S and let go of S, I still want W to read afterwards because it's still being held down. Right now, if I press and let go of S, I will lose W as well and the object stops, so you lose the "getkeydown" state...I'm guessing because getkeydown is not continuously being checked... sorry, i'm new at this.. so what would be the best way to check if a key was being held down before another key was pressed and let go?

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to Touch Drag 3D Objects 2 Answers

How do i move around in the editor without using arrow keys or middle click. 1 Answer

rotate Y axis to face mouse 1 Answer

How can I move an object physically correct around an other object? 1 Answer

Two Horizontal Movement Axes Behaving Differently 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges