• 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 Steelshot · Aug 02, 2016 at 12:04 PM · c#player

Choppy Player Movement with WASD Keys

Hi, when ever I wrote my script for the PlayerMovement I encountered something unusual to me. When the player presses the W or D keys he only goes either direction (forward or right) only once, but not continuously. This annoys me because I want the player to move continuously and smoothly across the platform. Any help? Thanks in advance :D

The speed variable is just a float with a value of 3f.

The Code:

         //Player will move up/down or left/right according to which keys he/she presses.
         if (Input.GetKeyDown (KeyCode.D))
         transform.Translate (Vector3.right * Speed * Time.deltaTime);
         if (Input.GetKeyDown (KeyCode.W))
         transform.Translate (Vector3.forward * Speed * 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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by TGrimace · Sep 06, 2016 at 06:18 AM

Input.GetKeyDown only fired when a key goes down. So to move forward, the player would have to hit the key over and over.

Input.GetKey fires as long as the key is pressed. So, you'd want to use that for continuous movement.

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
avatar image
1

Answer by Trickman · Sep 06, 2016 at 10:12 AM

You can simply use Input.GetAxis ("Horizontal") and Input.GetAxis ("Vertical") instead. It catches the input from both the arrow keys and the WASD keys.

I use it in this way:

     // Move the character
     Vector3 force = new Vector3 (Input.GetAxis ("Horizontal") * MOVE_SPEED, 
     0.0f, // This is the Y axis; I don't move in the Y axis unless I jump
     Input.GetAxis ("Vertical") * MOVE_SPEED);
 
     gameObject.GetComponent<Rigidbody> ().AddForce (force);
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.

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

7 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Player names all displayed as local player 1 Answer

Instantiate bullet towards Player position 1 Answer

Camera & Parent getting destroyed on Play 0 Answers

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