Simple Player Movement in Unity
Objective: Get the cube to move horizontally and vertically using WASD.
The first thing we need to do is access the cube’s position in the world, and to do this, we can use:
Transform is used to store an object’s position in the world, and Translate is what allows us to move the cube along a coordinate plane.
Now we need to access the X-axis and Y-axis of the coordinate plane. To do this, we can use:
With the new Vector2, we can assign an Input to allow the value of X and Y to increase and decrease with the press of the WASD/arrow keys on the keyboard.
Now we can move the cube left, right, up, and down! However, there is still a big problem:
The cube moves at about… A million miles an hour!
This is because the cube is running at 1 unit per frame. To fix this, we need to make the cube move at 1 unit per second with:
Much more manageable, but still way too slow for my liking. What we can do now is create a speed variable to give us the ability not only to increase the speed but set the speed to whatever we want.
Now the speed is right where I want it and if I wanted to change it more I can change it in the unity inspector.