- Home /
How to make the sprite move a certain amount of pixels when a key is pressed?
So im currently making a 2d game where you use wasd to move up, left, down, right on a chess like map and im confused on how I can make it where the sprite moves instantly to the next squares around it when you press one of the movement keys. All the tutorials ive been watching only shows fluid movement which is not what im wanting (Im also really new to coding in c# so there might be an easy solution to this, but I seem unable to figure this out).
Answer by NoahDaNerd · Oct 19, 2020 at 12:31 PM
You would not use pixels, you would use in-game units. Put this code in Update:
if(Input.GetKeyDown(KeyCode.W))
{
transform.Translate(Vector2.up * speed)
}
if(Input.GetKeyDown(KeyCode.A))
{
transform.Translate(-Vector2.right* speed)
}
if(Input.GetKeyDown(KeyCode.S))
{
transform.Translate(-Vector2.up* speed)
}
if(Input.GetKeyDown(KeyCode.D))
{
transform.Translate(Vector2.right * speed)
}
Your answer
Follow this Question
Related Questions
2D player keeps getting dragged to the left for some reason. 0 Answers
Player movement script for a stickman 1 Answer
How do i make a cube move (Continuosly without stopping) when i press a button once in unity 2D 2 Answers
[2D] Moving the player 1 tile at a time using rigidbody movement 0 Answers
Unity2D Space shooter rotation resetting when analogue stick input has ended 1 Answer