Player & movement in Unity
The ability to move your player - The fundamental of almost every game. But how is this done?
Well, it’s actually pretty simple! First, create a new 3D Object in Unity - we are going to be using a ‘Cube’ for this example.
Click on the Cube > And rename it to ‘Player’ in the ‘Inspector.’
Next up, we will be creating a ‘Player’ script. To do this, navigate to your ‘Project’ section and right-click on ‘Assets’ and ‘Create > Folder’ named ‘Scripts.’ Always keep your Hierarchy and Project sections as clear as possible! Next, right-click on the ‘Scripts’ folder and ‘Create > C# Script’ and name it ‘Player.’ Since we are already in the ‘Project’ section, we can also create a Material for our player - to do this, simply create a new folder named ‘Materials’ and ‘Create > Material’ in that folder. Name this material as ‘Player_mat.’
Next up, we will be changing the ‘Player_mat’ color to blue, but you can pick any color of your liking… and attach our material & script to our ‘Player’ game object.
Also, don’t forget to change your background color on your ‘Camera’ to a ‘Solid Color ‘ and choose your ‘Background’ color as well!
But how do we move an object in Unity? When we are talking about movement, we are talking about changing the ‘Transform’ component of the object, mainly the ‘Position’ attributes.
Let’s open up the ‘Player’ script — this should open the IDE of your choice; I am using Visual Studio Community 2019.
First up, we will be setting the position of our Player every time we run our “game.” We do this simply by setting its position in the ‘Start’ method.
Player Input - Input Manager
We can look at Input Manager; we can find it in Edit > Project Settings > Input Manager. Here we can see that Unity helps us immensely with the ability to check our ‘Horizontal’ and ‘Vertical’ axes for input; we can reference these to get our Player cube to move!
To get values from these axes is fairly straightforward. Since we’ll want to be moving anytime throughout the game, we will be tracking our input in the Update function loop.
In this code, we define two float-type variables and assign them the return values from the GetAxis method, where we pass arguments “Horizontal” and “Vertical” as a string for each axis. If you ‘Debug.Log’ these values, you will see that when you press A&D or W&S keys, the values will move anywhere from ‘-1 to 1', depending on the key pressed. This is what we are going to use to move our player!
Next up, we will be creating a new ‘Vector3’ variable, which we will call ‘direction,’ and we will pass the values of our float type variables as arguments for the x and y-axis.
And finally, to move our player, we have to use a method called 'transform.Translate’ — this is a method that tells our object to transform according to the values provided.
But as we can see, we are moving way too fast! This is because the Update loop is updating every frame, and we are running at 60 frames per second! This is why we have to make a slight adjustment to our code with ‘Time.deltaTime’ argument, and while we are at it, we will adjust the speed at which we will allow our player to move.
To adjust our player's speed, we will be creating a new ‘Serialized’ float variable.
Since we serialized this attribute - You can now adjust this value in the ‘Inspector’!
And finally, we are going to be modifying our ‘transform.Translate’ code with two new variables, and the resulting code is:
And there it is! You can now move your player freely in your game!
But that is it for now, thank you for reading and feel free to follow me for more articles - and as always, good luck and see you next time!