- Home /
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 Get$$anonymous$$ey ins$$anonymous$$d of Get$$anonymous$$eyDown
Edit: like robert said, using Get$$anonymous$$ey will make it go reaaally fast
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.
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.
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;
}
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;
}
}
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;
}
}
}
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.
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.
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
Your answer
Follow this Question
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