I like to move it, move it…
Today’s objective: move the player using the W, A, S, D keys
First I need to create an object to represent the player in our prototype. We will be dropping in a Cube primitive to represent our player.
I try to make it a habit to reset new objects transform when adding them.
Now we need somewhere to place our code to control movement, so we’re going to create and attach a new script to our player object. We’ll call it Player.cs as we’ll probably include other player-related code in the same location.
I’d like our player to start in the same location every time we run the game, regardless if we’ve moved the player in the Scene view for any reason. For this, we’ll utilize the Start method of our new Player script and use transform.position to explicitly set the Player’s location in the game space. I’m going to set the player to move to the center of the screen and down 2 units.
Ok, now let’s get our player moving! For this simple version of the player movement, we are going to use Unity’s input manager system to get the horizontal and vertical axis values. These axis values are mapped to a joystick, the W, A, S, and D keys, or the arrow keys on the keyboard. When we retrieve an axis from the Input manager we are returned a decimal value between –1 and 1 that represents the amount of movement in a given direction. The value returned is negative if moving left on the horizontal axis or down on the vertical axis and positive if moving right on the horizontal axis or up on the vertical axis. We’ll place this code in the update method so that it is being processed once per frame.
Now that we’re checking for user input let’s change the player’s movement based on that. We’ll use the Translate function of the transform object to move the player. We use Vector3.right as the base for our horizontal translation calculation because it is equivalent to
and multiplying our value from the horizontal axis will give us the proper direction for translation. For example, moving left at full speed on the horizontal axis yields a –1. multiplying the value of Vector3.right above by –1 yields
Which is a movement to the left. Likewise, we use Vector3.up for the vertical movement base value for the same calculation reasons but on the y axis. If we had used Vector.left or Vector.down as our base values our movement would have been opposite of the direction reported on each axis. This gives us the following code
And the following result when running in Unity
And now we have movement! Whoah! That’s some super speed! We are currently moving the user 1 unit in the direction determined by which key we are pressing per frame and we’re running at 60 frames per second! Let’s lock down the number of units the player can move per second by including
into our movement code. This takes our 1 unit of direction and multiplies it by the amount of time elapsed since the last frame to get how many units per second our object moves.
And our result when we run the game is:
But wait! That player is moving slow now! Let’s see if we can add some more control over the player’s speed? First, we’ll need a variable to store the current speed. Let’s set it to an initial speed factor of 3.5.
then we’ll want to include the speed variable into our movement calculations.
Which yields:
That looks much better. Let’s add some extra control from the Editor for ourselves, another dev, or a designer.
Which allows us to:
We now have a player that we can use with the W, A, S, and D keys, and we can adjust the speed at which the player object moves from the editor!
Next Time
There we have it! We have added a player, a player script, set the player’s initial location on the screen, and retrieved user input which we used to move the user with respect to the time between frames and a set speed variable that is adjustable from the editor! Tomorrow we’ll look at using Unity’s new input system to change this code and allow us some more flexibility in user input options! If you enjoyed this article, or want to come along with me as I progress on my journey, follow me at gamedevchris.medium.com.