Friday, August 5, 2022

Wall Run/Jump

 

Wall Run and Jump Set Up

Quick video to show how the script looks in action

First, in my movement script, I added a  wall run speed and a bool to check if wall running. I also added a wall run enum and in state handler, I checked to see if wall running and set the state to wall run and speed to wall run speed. 

I then made a wall run script and In it, I made a layer mask for the ground and for the wall as well as a float for wall run force the jump up and side force, max wall run time, and current wall run time. A float for the horizontal and vertical inputs. A float for how far the wall can be from the player a float for the minimum distance from the ground the player can be a ray cast and bool for the left and right. A bool to check if the character exits the wall and a timer to prevent them from just grabbing the wall again right after leaving it. And then a transform for the orientation a reference to the movement script and the rigid body.


In the start function, I grab the rigidbody and the movement script. In update, I call wall check which has the raycasts for the left and right side checking if there is a wall on either side which sets the respective bool to true if it hits a wall on that side. I also call a  bool to check if the player is above the ground so they cannot wall run while on the ground.


Then in the update function, I call the current state function which checks for the inputs, and if either one of the wall check bools is true and above ground start the wall run as well as the wall run timer if the wall run timer equals zero then exits the wall and exit wall timer becomes active so the player cannot just grab the wall again. Then if exit wall is true it stops the wall run and decreases the timer till the player can wall run again.


If the character is wall running it sets the movement scripts wall run bool to true and sets the wall run timer and makes the y velocity zero so the player does not keep flying upward. I called wall run movement in fixed update to work with physics correctly which first sets the gravity to false so the player does not fall and stays on the wall. I calculate the wall normals to find out the forward direction and if the orientation forward - the wall forward is less than the orientation forward - the negative wall forward the wall forward becomes negative so that the player goes forward and not backward on the wall. I also check to see if the player is trying to get away from the wall or not if they are not then add force forces the player to the wall. Next, I have the stop wall run script set the gravity to true and the movement scripts wall run bool to false. And in the current state portion if the player hits space then it calls wall jump which sets the exit wall to true and stops the wall run movement so the player does not get forced toward the wall since till the timer gets to zero the player cannot get back on the wall. I calculate the wall normal again and the force to apply set the y velocity to zero so the jump is consistent and adds force using the prior caculations.



Inspector Script Settings














No comments:

Post a Comment

My First Podcast

Nat20Coding · My Game Programming Thought Process I talk about my thought process behind making my game programs