Player Movement in Unity 2D
Day 4- Making 2D Space Shooter Game
When you are working with unity and you are making a game then there is bound to have some sort of player movement.
What we are going to look at is the very basic concept of making your player move in your game.
Lets start off by creating a gameobject in your scene which you will be moving around.
You can rename it as player to remember what you are trying to move. To make it look better, you can create a material and add it as a component to your player. If you don’t know how then you can check my other article ‘Working with GameObjects in Unity’
Now you can create a new C# script by right clicking on the folder you want to store the script into and selecting create and then C# script.
You should also note that after you selected the C# script, you are given a chance to rename your script so be sure to enter the name for your script before you hit enter. Since I am making my player move and it is going to be added to the Player, I am going to give it a name ‘Player’.
If you have already pressed enter then you will get ‘NewBehaviourScript’. You can change the name by right clicking on it and selecting Rename or F2 as shortcut. You successfully changed the name of your script but now you are going to get an error when you try to play the game.
What you are going to do to solve this issue is, go inside your script and change the class name same as your script name.
This should solve your issue but to avoid this hassle, it is best to remember to rename at the beginning or you can just delete and re-create again if no update was made.
This is my screen before starting the game and as you can see the player as on the side of the screen and not the middle or any position you would want your player to start at.
So if you add this line of code inside your Start method, wherever your player might be prior to the start of the game, it will reset the position to the origin i.e (0,0,0). You can manipulate the numbers to your choice but know that (This is x-axis, This is y-axis, This is z-axis).
Now if you add this line of code in your Update method, your player will move to the right with the speed of 1m/s. Vector3.right is telling the player the direction to move in, speed refers to a variable that is assigned a value which determines at what speed your player is moving and Time.deltaTime refers to the amount of time it took the player to complete the last frame to the current frame.
If you don’t multiply direction with Time.deltaTime then your player is going to move 1 meter per frame which means depending on your frame for example 60 times in 1 sec which totals to 60 meters in 1 sec.
You can also replace the Vector3.right by using new Vector3(1,0,0) instead.
If you go to Edit < Project Settings <Input Manager, you can see that there are axes and Unity provides you with list of different axes to map your user Input. Here, Inside the Horizontal axes, you can see that you can utilize left/right button and a/d button for left and right movement.
Now, you can create a float variable which stores the value of Input when you press left/right or a/d buttons. The value it contains is 1 for right and -1 for left so when you press d or right arrow key in your keyboard, the horizontalInput is going to be 1 when multiplied by Vector3.right remains positive and moves right but when you press a or left arrow key, horizontalInput is going to be -1 and when multiplied by Vector3.right, the direction becomes (-1,0,0) and it starts moving left.
For up and down movement, you an do the same but instead of Input.GetAxis(“Horizontal”), you are going to assign Input.GetAxis(“Vertical”) to the verticalInput variable; you are also going to replace horizontalInput with verticalInput as well as Vector3.right with Vector3.up which translates to move up when verticalInput is positive and move down when verticalInput is negative.
As you can see, the player is moving up/down and left and right.
If you want to know how to set bounds then read my player bounds in Unity 2D article