Day 6 — Getting the player moving

Connor Fullarton
6 min readMar 20, 2021

--

Hey and welcome!

Now we’re getting to my favourite part about Unity, the scripting. I immensely enjoy how it all fits together and that you can just attach the scripts to your objects to get them doing the behaviours you want.

Speaking of, drag and drop your Player script onto your Player object similar to what you did for the material and then open up the script in an editor of your choosing so that we can start on the movement behaviour for our Player.

Simply enter in the code above into your Player script and you’ll have a functioning player that you move about the game view. Pretty cool stuff right getting key presses to directly affect something in real time that you’ve made by yourself, I never stop being fascinated by the process of game creation.

I’m not letting you away that easy though, It’s one thing to copy and paste code you’ve found from googling but it’s another to know exactly what the code is doing, where it’s doing it and why it’s doing it.

I’ll just be covering what’s located in the Update() method for the time being, that private float _playerSpeed is a variable and I’ll be going into depth on variables in my next article. Also the green text are comments and C# recognises to ignore that line when the script is running.

void Update()
{
// Your code here.
}

Let’s start with the Update() method, that’s a global Unity method and will be one of the two starting methods you have when you first create a script alongside the Start() method. The Update() method is one that’s called upon every frame in your game, so if your game is running at 60fps then this method is getting called 60 times a second. This is particularly handy when it comes to user input since it will constantly be checking for any key pressing or button pressing done and will react accordingly instantaneously.

Next up we got some local variables with the data type of float which is our horizontalInput and verticalInput, again there will be plenty of time for variables in the next article so let’s focus on what’s been assigned to these values. First there’s Input.GetAxis(“Horizontal”) which will get a positive or negative value of 1 depending on whether the A key or the D key has been pressed. If you want to see that in action you could add in Debug.Log(Input.GetAxis(“Horizontal”) into your Update() method and you’ll see in the console that when you hold the A key the console will show a number that gets lower until it hits -1, and the same for the D key except it the number will rise to 1.

If you don’t have the console window in your Unity editor you can get it from Window > General > Console and then drag it somewhere in the editor to your liking.

Now Input.GetAxis(“Vertical”) works the same way but this time it produces a negative or positive value of 1 depending on whether the W or S key has been pressed. You’re probably wondering where something like this gets defined so if you head over to Edit > Project Settings > Input Manager > Axes then expand the Horizontal and Vertical sections and you should be seeing this:

Starting with horizontal, if you check out the Alt Negative Button and Alt Positive Button you’ll see that this is where the A and D keys are being specified with negative resulting in -1 and positive leading to 1. This horizontal section is where our Input.GetAxis(“Horizontal”) is looking for its values. You’ll also notice that it’s tracking for inputs from the left and right arrow keys as well meaning we can use those for movement. The best part about this is that we can change the values to our liking.

The vertical section is covering much the same here with the only difference being the assigned keys. With that covered it’s time to go over how this all fits together in our last bit of code.

I hope you’re as excited as I am right now because it’s a great feeling figuring out what goes where and the dot connecting together. To start off with we’re defining a variable of direction with a Vector3 data type. After that we’re then assigning the variable with a new Vector3 value here and some of this will look familiar to you as our local variables for the horizontal and vertical inputs are being put to use.

A Vector3 consists of three float values of x, y and z looking like this Vector3(x, y, z) and for the purpose of this it’s used to determine the player’s direction in a 3D space. So let’s say we’re pressing down the A key, we know that results in -1 so our Vector3 will look like Vector3(-1, 0, 0) which means our player is moving on the x axis toward a -1 direction, better known as left. Likewise if we’re pressing the W key our player is moving in a positive direction along the y axis, better known as up for us.

Last line now, let’s take a look at transform.Translate() first. I haven’t spoken about it yet but every game object you create has a transform component which you may have spotted in the inspector when you changed the name of your player object.

Now running transform.Translate() is going to be altering the position values here of our player object depending on the translation we pass into the brackets. Well we know all about the direction part of the translation here but if you were to run transform.Transform(direction) you’ll notice that the player goes in the direction you’re after but will be going mach 1. That’s where the next part of the translation comes in handy as it will be handling the player’s speed.

Currently our speed is directly tied to the framerate in the game, so if we’re moving to the right we’ll be moving at 60 metres per second at 60fps which is pretty fast for our game here. We can solve this with Time.deltaTime which brings our speed down to real time where we’ll be moving 1 meter a second now. With that in place we can now alter our speed using our _playerSpeed variable which is currently set to 8. So multiplying what we have now by our _playerSpeed will set the overall speed of our player to 8 metres per second.

And that’s all she wrote. Quite a long one this ended up being but I wanted to stress the importance of making sure that you know exactly what each bit of your code is doing as going on a mad copy and pasting spree can lead to gaps in knowledge when it comes to programming.

Next time we’ll be looking into those variables I keep talking about.

--

--

Connor Fullarton
Connor Fullarton

Written by Connor Fullarton

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.

No responses yet