Beginning with player movement in Unity
Today I’m working on creating a simple movement system for my player in the Space Shooter game. Can’t have a game without allowing your player to move, can we? Right now our player is nothing more than a humble, cyan cube.
I created a C# script and attached it to the player object. I want to set the cube’s position in 3D space when the game starts up, so I’ll do that in the ‘Start’ function of the script. I’m setting the position value (x, y, z) of the cube’s transform component to be 0, 0, 0 in 3D space.
That’s cool and all, but we still need to make the cube move. I’ll do that in the ‘Update’ function. I’m using the .Translate function to change the position of the cube’s transform component each frame. Vector3.right is a built-in Vector3 value that will make the cube move to the right when passed to the Translate function. I am then multiplying the Vector3 by the float variable _playerSpeed and by Time.deltaTime to regulate the speed at which the cube moves.
Time.deltaTime is used to normalize the speed at which objects move to make their speed relative to the actual time that has passed, rather than on the framerate of the game.
Look at it go! Next we need to allow the player to control the cube’s movement. We’ll look more at that next time, when we figure out how to read input values and incorporate them into our game logic.