2D Wall Jumping in Unity / 2021 (Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to the channel last time i showed you how to set up a simple 2d platformer player controller script and in this episode i want to add wall jumping and wall sliding so the first thing we want to do is go back to our player and select the player movement script that we created last time once you have it open in visual studio i'm just going to go ahead and adds a few comments and call this movement and jump variables and just below this i'm going to add some wall jump variables now what we're doing for the wall jumping is going to be similar to what we do when we're detecting the ground for our regular jumping uh we're going gonna have multiple raycasts uh just one on each side of the player so to the left and the right and it's gonna detect if it's intersecting with a wall and if it is if the player is holding the key pointing toward the wall then they will start sliding down it then if they press the space button then they will jump away from the wall so first let's go ahead and define a few variables i'm going to do a private bool on wall right private bool on wall left a vector 3 called wall ray position left another vector three for wall ray position right and a couple of raycast hit 2d arrays first one called wall hits left i'll just copy this instead of typing it all out our wall hits right so first thing i want to do is actually last time we put our jumping code inside our movement function that doesn't make a whole lot of sense so i'm going to cut that out and i am actually just going to put it down here in the jump function i will also add a comment that this is the ground jump the next piece of uh cleanup i want to do is with all of these rays since we're adding a couple more i think it makes sense to maybe just add its own function for setting up the raise so i will call that ray setup what i'm going to do is i will just copy all of those into there and remove them from the jump function and just call the race setup function inside at the top of the jump function where all of these were before i will also go ahead and add a comment saying that these are the ground check slash standard jump ray setup and then down below here i will define or comment for our wall jump slash slide ray setup and i'm going to go ahead and add the wall ray position left equal to transform dot position plus a new vector 3 where the x position is the negative ray position offset which we defined in our previous video at half at 0.5 which is roughly half the height of our character and 0 for the z since it is a 2d game i will go ahead and copy that and just call it walray position left and remove that negative sign next thing i wanted to find is wall hits left set that equal to a physics 2d ray cast all at position wall rate position left vector 2 dot left and the length will be ray length which we defined last time as well and i can just take this and paste it or wall hits right and just make the two dot left negative and it should point the opposite way as well as change the position to wall bray position right next thing i want to do is i will say on wall left equals ray check of wall hits left so what ray check is going to do is we're going to be able to pass in these wall hits left or right which is uh an array of raycasts that store all the colliders that the ray interacts with and when we send this uh similar to to a function we have before it's going to return true or false if it is interacting with something that's not the player so let's go ahead and actually define that as a private pool raycheck and i will actually take the code from our ground check and just copy that and we can adjust this so i think we can get rid of that outside one what we want to pass is a raycast hits 2d array and we can call that ray hits and i will say for each hit in ray hits if the clutter is not the player collider then we'll return true if not we are returning false so since we got the left side checked out we also want to do it for the right so while hits right on wall right and while we're at it i kind of want to change or update the ground check function to make it a little more general so what i want to do is i'll right click go to rename i'm going to call this ray list check and i'll rename our ground hits to ray hits and that way it makes it a little more general so in the future if you have any more rays that you want to make an array list or an array and an array for raycast hits uh you can pass it to the arraylist check function and uh i don't know it's a little it's a little cleaner it's not super necessary but i like it another bit of cleanup i want to do is i want to add a function to draw the rays in the scene so it's a little easier to control when we want to draw the rays like right here i have to put these symbols on to comment it out when i don't want to but if i make it one function then i only have to comment out one line so i will make it so right here we put the ground rays which i believe are these ones i can take that out and then i will want to draw our wall rays which will be coming out of the sides of the player so to do that i'm just going to copy one of these lines i will then copy this piece of code from our wall hits actually i'll do wall hits left first and just copy paste and i will make these blue i can then copy this line bring it down there flip the vector2.left by putting a negative sign and call the position right so now whenever we want to draw our rays we can just call this function i'm going to go ahead and add our draw raise function here did i misname it i certainly did so now at the end of our race setup we are drawing the rays and if i didn't want to i could just comment out this one line so i'm going to save and go ahead and check on how we're doing so far in unity we can go ahead and run our game and then we can click on gizmos move our player around and as you can see we have our ground rays and our wall rays our jumping and our movement seem to be working so let's go ahead and add our wall slide so one more thing before we add our wall slide we need to add a bool to check if we are on the ground so i'm gonna go ahead and go back up top and i will add a private bool and call it on ground we can also get rid of this can jump equals true since we have our rays in there to calculate that for us and one more rule we're going to want to add is a private bool i'm going to call it can move i'm going to set that equal to true and you'll see uh what we need this for a little bit later all right so going back to our jump function i'm going to add an if statement it says if we're not on the ground check if we can move if input dot get x is raw of the horizontal axis is less than zero and we are on wall left then i will set our rigid body's velocity equal to a new vector 2 with rb dot velocity dot x for the x value so that doesn't change to a negative wall slide speed variable which we will also need to define if we go ahead and copy our wall slide speed we can go back up to the top and i will define it right here alongside our movement and jump speed so i will serialize the field make a private float paste our wall slide speed that way we can play with the values in the editor i will also add an else if i will copy our previous statement on wall right and we'll make this greater than zero then it is essentially the same thing i'm breaking it up like this so it's easier to read uh you could just do if this or if this one really results in the same thing but i do this for readability so now we should have our wall slide ability let's go check it out i'm going to go ahead and set our wall slide speed to 1.25 and i'm also gonna take a second and change up our scene a little bit so it's a little more wall slide and wall jump friendly all right so i'm going to reselect our player and now we can try out our wall sliding so if we go ahead and jump and i'm holding the key against the wall we're sliding down that looks good if i let go then we'll fall at our usual speed so i actually realized something uh we added the on ground variable but we didn't actually set it up so what i'm going to do is i'm going to set on ground equal to ray list check i'm going to pass in all raycast hits and that will return a bool depending on if we are touching the ground or not then i am also going to set can jump equal to on ground and that is just for the uh the normal jump ability and i'm going to go inside of our jump function and remove this line for the can jump equal to the arraylist check uh it's doing the same thing but we're calling it up here in a different function so it's redundant and we can remove it all right so the next part that we are going to want to do is we are going to add our wall jump ability and to do this i'm going to say if input dot get key down of keycode dot space and on wall left and rb our rigid bodies velocity will equal a new vector 2 the x direction i'm going to say jump speed times 0.5 just to cut it in half and the y value is just going to be our jump speed so that way we jump at kind of a at an angle away from the wall and i'm also going to add in a little bit of cool down so i'm going to add an ie enumerator called wall jump cooldown and i'm going to set can move equal to false for a fraction of a second just so after we jump away from the wall we don't immediately move back due to our input let's say yield return new wait for seconds and i'm just going to put in a quarter of a second then can move will equal true and what we want to add as well in our movement function is an if statement it says if can move then we will run all of our movement code and to start this cooldown we will go ahead and add a start co routine wall jump cool down so after we jump away from the wall we'll start our co routine to start our uh wall jump cool down so we don't move immediately back to the wall after a quarter second we will regain control and that i find that's just about enough time to allow for the player to do any sort of movement they want after a jump and i'm just going to go ahead and copy that and add it again but this time for the right side but i will add a negative sign to the x direction so we jump the other way with all that checked i think we are ready to go ahead and test i saved my code going back to unity let's see how we do hopefully i didn't miss anything go here it looks like we're working one thing with this code and it could be an improvement for later if you are only on if you're only on one wall and you're jumping you see you can actually climb and scale the wall pretty effectively with only using one side if you want to prevent that i'm sure you can find a way around it i didn't want to put the time into it right now just because i wanted to keep it simple maybe that'll be a future video if enough people want it but for right now i hope you enjoy your simple wall sliding and wall jumping code that we added to our simple 2d platformer player controller script thank you for watching if you like this video leave a comment and a like telling me what i should do next and feel free to subscribe thank you very much bye
Info
Channel: MichaelsGameLab
Views: 1,652
Rating: undefined out of 5
Keywords: gamedev, unity, tutorial, michaelsgamelab, c#, programming, art, animation, platformer tutorial, wall jump, wall slide, beginner tutorial, coding, player controller, 2D physics, unity2d, unity3d, 2D, learn unity, unity games, super meat boy, unity wall jump, unity wall jump 2d, game, developement, dev, unity wall slide, colliders, rigidbody, unity tutorial
Id: 1Iv8Z00fhDY
Channel Id: undefined
Length: 20min 49sec (1249 seconds)
Published: Fri Apr 09 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.