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 !

try GetKey instead of GetKeyDown Edit: like robert said, using GetKey will make it go reaaally fast

–

4 Answers

4

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.

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#

–

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

–

OK, 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; }

–

Here is a complete C# translation: using UnityEngine; using System.Collections; public class Move : MonoBehaviour { float speed = 1.0f; void Update() { var move = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0); transform.position += move * speed * Time.deltaTime; } }

–

That's so awesome! Thanks a lot for all your help !!! :)

–

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.

[103836-capture2.png|103836]

–

You can use Input.GetKey( ) instead of Input.GetKeyDown( ). It will make a continuous movement.

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 :smiley: