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 /
  • Help Room /
avatar image
3
Question by Vandrcommander · Jun 14, 2017 at 02:36 AM · movement script2d spriteswasdarrow-keys

WASD/Arrow key movement

I have looked around on the forums, and I can't seem to find a script that allows a 2D sprite to be moved with WASD or the arrow keys. I'm rather new to Unity, can someone please share a script with me or point me in the right direction? This is the script I am currently using`using UnityEngine; using System.Collections;

public class Ctrl : MonoBehaviour { public float speed = 1.5f;

 void Update ()
 {
     if (Input.GetKey(KeyCode.LeftArrow))
     {
         transform.position += Vector3.left * speed * Time.deltaTime;
     }
     if (Input.GetKey(KeyCode.RightArrow))
     {
         transform.position += Vector3.right * speed * Time.deltaTime;
     }
     if (Input.GetKey(KeyCode.UpArrow))
     {
         transform.position += Vector3.up * speed * Time.deltaTime;
     }
     if (Input.GetKey(KeyCode.DownArrow))
     {
         transform.position += Vector3.down * speed * Time.deltaTime;
     }
 }

}`

Comment
Add comment · Show 1
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 Durstie · Sep 19, 2020 at 02:47 AM 0
Share

I don't know about for 2D, but in 3D I've been using the new inputs feature with callbacks instead of listening for keycodes manually.


With the new system, you create an action and assign as many bindings to it as you want. Then, when one of those bindings is triggered a specific function in your script can be triggered.


Pseudo example:
Action: $$anonymous$$ovement
Bindings: Left Stick, WASD, Arrow_Keys
Callback Function: YourClass.$$anonymous$$ove()
Pseudo code of your class:

   public void $$anonymous$$ove(InputAction.CallbackContext context)
   {
     moveVector = context.ReadValue<Vector2>();
     if(moveVector.magnitude != 0)
     {
       // Do your movement stuff here
     }
     else
     {
       // Do your end walking stuff here
     }
   }

That basically says:

  1. Save the button press info to a Vector2

  2. If the Vector2 is not (0,0) do some movement stuff

  3. If the Vector2 is (0,0) do some other stuff that would likely be the character co$$anonymous$$g to a stop


context contains all kind of useful stuff, not just is a button pressed. In the example context.ReadValue() says to read the x,y coordinates of that action where x and y can be -1, 0, 1 (or any float in between). This lets you know if you are pressing a button to walk forward, backward, left, right, or some combination.


To use the new input system, you'll need to install the package and enable it: Input System Installation


Once you have it installed, here's the quick start guide: Inputs Quick Start


I know listening for keys and acting on them is probably more straight forward than this, but this has the benefit of updating keybindings much simpler in my opinion.

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by KBCreater · Jun 14, 2017 at 03:40 AM

@Vandrcommander This may or may not help

     public float panSpeed = 20f;
 
     void Update () {
         Vector3 pos = transform.position;
 
         if (Input.GetKey ("w")) {
             pos.z += speed * Time.deltaTime;
         }
         if (Input.GetKey ("s")) {
             pos.z -= speed * Time.deltaTime;
         }
         if (Input.GetKey ("d")) {
             pos.x += speed * Time.deltaTime;
         }
         if (Input.GetKey ("a")) {
             pos.x -= speed * Time.deltaTime;
         }
             
 
         transform.position = pos;
     }

You would have to use wasd in this version and is usually standard in most games. I could not fix the arrow key problem even after trying. Some one else would have to help you with that. Just drop the code into the game object and you should be good.

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
0

Answer by RpxdYTX · Mar 28, 2021 at 08:10 PM

what about transform.Translate() ?

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

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

Related Questions

How do I move 2 standard assests cars, one with WASD keys and one with Arrow Keys 0 Answers

My C# gave me inverted controls for left and right WASD movement? 1 Answer

WASD and camera movement 1 Answer

Vertical Movement Is Wierd 1 Answer

Distance between projectile and player ? 1 Answer


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