Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
8
Question by coolbird22 · Mar 19, 2014 at 05:39 AM · 2dobjectmoveaccelerometer

How do I move my 2D object using arrow keys ?

I found a few similar asked questions but the solutions for which gave me errors while compiling.

Also, a code I used to move it using the arrow keys made it move just 1 unit per key press, despite it being in the Update() snippet, which just didn't make sense to me. The code I used is as follows:-

 using UnityEngine;
 using System.Collections;
  
 public class Ctrl : MonoBehaviour
  
 {
         void Update ()
         {
                 if (Input.GetKeyDown(KeyCode.LeftArrow))
                 {
                         Vector3 position = this.transform.position;
                         position.x--;
                         this.transform.position = position;
                 }
                 if (Input.GetKeyDown(KeyCode.RightArrow))
                 {
                         Vector3 position = this.transform.position;
                         position.x++;
                         this.transform.position = position;
                 }
                 if (Input.GetKeyDown(KeyCode.UpArrow))
                 {
                         Vector3 position = this.transform.position;
                         position.y++;
                         this.transform.position = position;
                 }
                 if (Input.GetKeyDown(KeyCode.DownArrow))
                 {
                         Vector3 position = this.transform.position;
                         position.y--;
                         this.transform.position = position;
                 }
 }
 }

Thanks for being such a wonderful community !

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 Destran · Mar 19, 2014 at 05:56 AM 0
Share

try Get$$anonymous$$ey ins$$anonymous$$d of Get$$anonymous$$eyDown

Edit: like robert said, using Get$$anonymous$$ey will make it go reaaally fast

4 Replies

· Add your reply
  • Sort: 
avatar image
14
Best Answer

Answer by robertbu · Mar 19, 2014 at 05:57 AM

There is all sorts of different kinds of movements. And there are a bizillion movement scripts posted on UA. So here is a bizillion+1 script:

 #pragma strict 
 
 var speed : float = 1.0;
 
 function Update() {
     var move = Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0);
     transform.position += move * speed * Time.deltaTime;
 }

It assumes you've left 'Horizontal' and 'Vertical' axes at their default values. Note your script at the link only moves once because you are use Input.GetKeyDown(). GetKeyDown() returns true for only the frame the button goes down, so you are only getting one movement per keypress. You can 'fix' this by changing to Input.GetKey(), which returns true all during the time the key is held down. Note that since you are increment your position by 1 (i.e. using '++"), going to GetKey() will make the object move really fast...likely around 60 units per second.

Comment
Add comment · Show 13 · 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 coolbird22 · Mar 19, 2014 at 06:21 AM 0
Share

Thanks robertbu & Destran for the speedy reply.

@robertbu - By saying "It assumes you've left 'Horizontal' and 'Vertical' axes at their default values.", I don't quite get what that meant. Is it so that the axes don't move and stay in the middle of the screen or something else. Also, on using your script, I'm getting errors in the Unity console that says

  • Unrecognized #pragma directive

  • Unexpected symbol ':' in class, struct, or interface member declaration

  • Unexpected symbol '=' in class, struct, or interface member declaration

  • Parsing error

I'm using C#

avatar image Destran · Mar 19, 2014 at 06:23 AM 0
Share

the error would be because your linked script is in C# and he wrote his in Javascript.

avatar image coolbird22 · Mar 19, 2014 at 06:30 AM 0
Share

O$$anonymous$$, I managed to convert it to C#. Stupid, silly me. But, I'm still getting that Parsing error that points to the last curly bracket for some reason.

 float speed = 1.0f;
     
 void Update() {
         var move = Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0);
         transform.position += move * speed * Time.deltaTime;
 }
avatar image robertbu · Mar 19, 2014 at 06:45 AM 1
Share

Here is a complete C# translation:

 using UnityEngine;
 using System.Collections;
 
 public class $$anonymous$$ove : $$anonymous$$onoBehaviour {
     
     float speed = 1.0f;
     
     void Update() {
         var move = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0);
         transform.position += move * speed * Time.deltaTime;
     }
 }
avatar image robertbu · Mar 19, 2014 at 07:13 AM 1
Share

To compare and contrast, here is a bit of rewrite to your original code that accomplishes approximately the same thing:

 using UnityEngine;
 using System.Collections;
 
 public class Ctrl : $$anonymous$$onoBehaviour
 {
     public float speed = 1.5f;
 
     void Update ()
     {
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow))
         {
             transform.position += Vector3.left * speed * Time.deltaTime;
         }
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow))
         {
             transform.position += Vector3.right * speed * Time.deltaTime;
         }
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.UpArrow))
         {
             transform.position += Vector3.up * speed * Time.deltaTime;
         }
         if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.DownArrow))
         {
             transform.position += Vector3.down * speed * Time.deltaTime;
         }
     }
 }
Show more comments
avatar image
0

Answer by ricochetv1 · Apr 28, 2017 at 05:00 AM

Probably better to capture whatever Unity uses for a keydown event. I'm actually looking for the way to do that right now, 'cuz it's apparently different from the standard C# implementation. In 4.6.9, anyway... Haven't gotten in to 5 yet.

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 KhizarRaza · Oct 16, 2017 at 10:58 AM 0
Share

alt text

capture2.png (37.9 kB)
avatar image
0

Answer by dipannita · Jul 19, 2017 at 07:41 AM

You can use Input.GetKey( ) instead of Input.GetKeyDown( ). It will make a 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
0

Answer by Jennifer907 · Mar 29, 2018 at 05:27 PM

Thankyou @robertu , i was also looking for the solution to move the player with a simple small script and there i saw your comment which helped me to move my player :D

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

28 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

Related Questions

move UI object to click position 1 Answer

Moving an object along a prespecified trajectory according to coordinates from arrays 2 Answers

2D Character grabing the wall 1 Answer

How do I add gravity to an object? 1 Answer

2D Movement Problems 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