How To Wall Slide & Wall Jump In Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
while sliding and wall jumping have been the most requested features on this channel so far so without further Ado let's roll up our sleeves and finally make your wish come true I prepared a sample scene with a controllable player and a few walls each with a Sprite renderer and a box collider 2D let's create an empty game object called wall check as a child of our player and position it on the right edge of our player's Collider If your player faces left by default move the wall check to the left side of your player let's look at the script attached to our player and Implement wall sliding first if you have seen my player movement tutorial the script should already look familiar first of all we need two private variables a Boolean that indicates whether we are wall sliding and the float for the wall sliding speed we also need two serialized fields to reference the wall check and the wall layer we are going to add the wall layer later in unity let's create a private method called iswald that returns a Boolean check if we are touching a wall we use a method called physics2d.overlap Circle [Music] this method creates an invisible circle with a radius of 0.2 at the position of the wall check and returns true if it collides with the wall layer let's create another private method called wall slide [Music] so if this world returns true and is grounded returns false and the horizontal input is not zero we set this wall sliding to true otherwise we set this wall sliding to false and once is Fault sliding is true we clamp the rigid body's Vertical Velocity between the negative value of the wall sliding speed and float.max value last but not least we call the wall slide method inside the update method let's save our script and go back to Unity back in unity we need to add a new layer called wall and assign it to every wall in our scene foreign [Music] next we need to reference the wall check and the wall layer in our script [Music] by the way you can also select multiple layers here but that's entirely up to you we play the game now we should be able to wall slide nice now it's finally time to add wall jumping to our script first of all we need a few more private variables a Boolean that indicates whether we are wall jumping a float for the wall jumping Direction a float for the wall jumping time float for the wall jumping counter float for the wall jumping duration and the vector 2 for the wall jumping power let's create a private method called wall jump so whenever is wall sliding is true we set its wall jumping to false next we set the wall jumping direction to the negative value of transform.localscale.x in our case transform.localscale.x indicates the direction our player is facing and we want to jump in the opposite direction the different method to flip your player and need help adjusting the code accordingly feel free to leave a comment or join our Discord server [Music] now we set the wall jumping counter to the wall jumping time and if his wall sliding is false we make the wall jumping counter count down by subtracting time dot Delta time from it this allows us to turn away from the wall and still be able to wall jump for a brief moment afterwards if we press the jump button and the wall jumping counter is greater than zero we set its wall jumping to true [Music] next we set the rigid body's horizontal velocity to the wall jumping Direction multiplied by the X component of the wall jumping power and the rigid body's Vertical Velocity to the Y component of the wall jumping power to prevent our player from jumping multiple times by spamming the jump button we now need to set the wall jumping counter to zero we always want to face the direction of movement so we flip our player if transformed that localscale.x is not equal to the wall jumping Direction to flip our player we set is facing right to its opposite value and multiply the X component of transform.local scale by -1 again if you flip your player differently you'll probably need to change the code a little bit let's create another private method called stop wall jumping and set is wall jumping to false foreign method once this wall jumping is true we invoke the previously created method with a short delay [Music] and if his wall sliding is true we cancel the invocation of stop wall jumping [Music] the wall jump method is done but we still need to call it inside the update method last but not least we don't want our player to be able to move and flip while while jumping so we only execute the responsible code for moving and flipping if is wall jumping is false [Music] let's save our script and go back to Unity you play the game now we should be able to wall jump [Music] awesome [Music] there are many different types of wall slides and wall jumps and there are even more ways to implement them in unity but if you are happy with my solution please leave a like and consider subscribing thank you for watching and I'll see you next time
Info
Channel: bendux
Views: 54,120
Rating: undefined out of 5
Keywords: bendux, unity, tutorial, 2d, wall slide, wall sliding, wall jump, wall jumping, indie, game development, how to
Id: O6VX6Ro7EtA
Channel Id: undefined
Length: 6min 38sec (398 seconds)
Published: Wed Oct 26 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.