Player Movement :
Simple player movement:
We created our first script, now we are going to write code for player movement in it. Drag and drop this script on player Object so that we can use it for it.
There are several ways to make code for player movement and we can add different logic for different functionality. In this project, we are going to move our Cube, like in a 2d game that is Up-Down-Left and Right.
Now we are adding key controls to the code to move our cube,
In this code, we added playerSpeed variable to hold value for movement speed for the player.
In the MovePlayer function, we are getting user inputs and moving players as per those inputs.
Input.GetAxis will give us the value for a particular axis. it may be +1 or -1.
ModifiedCheckBoundary() explained below.
In the above code, we are moving the player on a 2d plane. now we need to add some restrictions for the cube so it will not go outside of the camera view. for that purpose we need to create variables that will hold the value for x and y boundaries, that is minimum value and maximum value for X-axis and Y-axis.
Now that we got values for the x and y-axis boundary, we can write code for a player boundary.
With the above code, we can stop the player from leaving the camera view. now, the player not be able to go beyond camera view. and always stay in our boundary area.
We can add more code that will teleport the player to the top when he crosses the bottom boundary or left side when reaching the extreme right corner.
Now we write code for both scenarios wherein first we stop the player from moving outside and in the second we teleport the player to the opposite direction if he tries to go outside. We can use both codes in different conditions. for that add bool variable and write code as below,
We can use any one condition for the boss level or for the special level…