We all have to begin somewhere and Game Development is no different. In fact, even a new project is no different. so one of the things to do when designing your game is to work on the mechanics of your game and one of the first mechanics that almost any game has is player movement (even if you are building checkers you still need mechanics to move those pieces around).
So before you get those fancy art assets that your friend is promising to make, start making your game with primitives, like cubes and spheres, and design your player movement. So once you’ve got your lovely player cube in the scene and make sure it in the middle of the screen add a player script and let’s add some code.
Now the Beuty of working with a Game engine is a lot of the work has been done for us and we don’t have to write dozens of lines of code just to read the input from keyboard or mouse or gamepads. Unity has a handy Input System that we can use to get if the user presses the horizontal or vertical inputs (so arrow keys, WASD key, or the controller Joystick) and we can capture that input with two lines like this.
this takes the float value (don’t worry I’ll get to what a float is incoming articles, it’s just a decimal number) from Unity’s “Input” class for the two Axis Horizontal and Vertical. So if you push your joystick to the left you won’t go left just yet as you need to tell the engine to move the cube in the direction in the float variables we do that by converting our input into a vector.
Now our vector will not move our cube either but we are getting closer. Now with Unity, every game object has a Transform component (this stores its position scale and rotation), We want to change the position of our player cube and show that movement on the screen. Luckily for us, the Transform component has a handy method called Translate. Translate takes the vector we stored above and moves the cube in the directions stored inside. now the Unity update loop can loop hundred times per-second and if we just give some speed and direction without taking into account that in one second there could be 60+ movements, our cube could fly off the screen. So we multiply by Time.deltatime (this the elapsed milliseconds since the last loop) to give us a more reasonable speed.
And that is it 4 lines of code and we start having fun moving Cubie around the screen.