My journey becoming a Unity game developer: 2.5D Infinite Runner-Player using an elevator and wall jumping
Objective: Player need to collect 10 coins to activate the elevator. Also, keep the player jumping in the forward direction only, and allowing the player to jump from one wall to another to climb up.
Let’s quickly setup the Player to collect coins in this scene. From the previous scene setup we created scripts to allow the Player to collect coins, increment coins count, and update coin amount display onscreen. To see the scripts involved in setting up the Player collection of coins, go to the following link: https://rhetthaynes66.medium.com/my-journey-becoming-a-unity-game-developer-2-5d-infinite-runner-coin-collection-and-display-c078b1fab648
The only thing we will add-on to the Player’s ability to collect coins is that the Player must collect 10 coins to activate the elevator so they will be allowed to go up to the next floor.
Create a new method which is Public Type Int name CoinsCount(). This method will return the coins collected by the Player.
Inside the script name Elevator Panel. We will create a variable either Public or Serializefield Type Int name requiredCoins. RequiredCoins variable will be set to a default of 10 to represent how many coins the Player must have to activate the Elevator game object.
In the OnTriggerEnter() function where we checked IF the other’s Tag or CompareTag which is better to use is Player, and the “E” key has been pressed. We want to check the that the “E” key is pressed, AND get the other’s component belonging to the Player so we can get the CoinsCount() method to check how many coins the Player has. IF the CoinsCount() of the Player equals the required Coins needed, the Player can activate the elevator to use.
Take the Coin prefab and duplicate at least 10 of them to test. Position them across the floor for the player to collect.
To see how many coins the player collected displayed onscreen, select the Canvas object. Place the UI Manager script on it, then drag the Coin text object and Lives text object into each slot assigned in the UI Manager script component.
To create the UI Manager script, go to the following article: https://rhetthaynes66.medium.com/my-journey-becoming-a-unity-game-developer-2-5d-infinite-runner-coin-collection-and-display-c078b1fab648
When we play the game, we tried to access the elevator having only 3 coins. As we can see the elevator didn’t move, but when the player collected 10 coins the panel’s light changed from red to green and the elevator moves down to the lower floor.
Now we want to limit the Player’s ability to jump forward only, not backwards. Move the direction and velocity variables inside the IF controller is grounded condition. This way we only want the Player to move forward and back when they’re on the ground. Also, the Player will only jump in the direction the user presses on the keyboard.
To set up the Player’s ability to jump off a wall, let’s use the OnControllerColliderHit() function. This will check if the Player object’s controller has hit another game object’s collider while performing the controller’s Move() function.
Inside this function, we want to check IF the Player’s controller is not grounded AND the collider the player hit belongs to a “Wall” by checking the transform of the object and checking if the Tag assigned is set to “Wall.”
We will start by testing to see if the player is colliding with any walls by using the Debug DrawRay() method. The 1st parameter we will check the impact point in world space where the ray hit the collider. The 2nd parameter will check the normal of the surface the ray hit with the direction pointing away from the collider it hits forming right angles. The 3rd parameter will draw a line showing impact in the color red.
Next we will use the new variable created Type Vector 3 name wallSurfaceNormal. We will use this variable to hold the actual normal surface the Player will jump off from. Finally, we will set the canWallJump to True which will allow the Player to jump off the wallSurfaceNormal objects tagged “Wall.”
Still in the Player script, down in the canDoubleJump condition we want to check IF the “Space” key is pressed AND canWallJump is False. If False, the player will do just a regular jump. Then, check IF the “Space” key is pressed AND canWallJump is True. If True, the player can do a regular jump and using the velocity variable, the Player can jump off a wall by setting the variable to wallSurfaceNormal multiplied by speed.
In the Scene View, select the objects the Player will be allowed to jump off from, and add a “Wall” Tag to each object.
The player can jump off the walls by reflection. However, there is a problem with this wall jump as the Player jumps, it still is considered even from a distance to be hitting the wall. Therefore, the Player will be jumping constantly in the opposite direction from the nearest wall each time the “Space” key is pressed.
To fix this jumping issue, simply add the canWallJump variable to equal False before the Player is influenced by gravity under the yVelocity. This will allow the Player to jump off a wall, and if there is no other walls available for the Player to jump off of they will not be able to wall jump anymore until they come back to a wall again.
Player can wall jump from one wall to another, but now if there is only one wall available, the Player will only be able to jump off the wall one time only until they land on the ground. You will notice that if the player use a single jump to land on the wall, they can double-jump off the wall. If they double-jump onto the wall, they will be able to do a single jump only off the wall.
Next time we will work on Pushing the Box onto the Pressure Pad.