Easy Wall Jump & Slide in Unity | 2D Game Dev Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's a fella game fupa's my name is Tyler Potts and welcome back to muddy wolf studios and in this video we are going to be creating a basically a wall jump slash wall sliding little thing it's super easy to do wench grappling onto a wall you could probably add some particle effects could jump off them and stuff like that's only be showing you how to make this whole thing basically from scratch so without further ado let's get started I'm going to open up a new scene okay guys I paint new scene just so you saw it before I've also added the movement script and say it all up I'm not going to go over the movement script too much in this video but you can see here we move like this we do get stuck to this wall and we haven't actually got our script our jump our wall jump script on and the reason for that is because we need to add a physics element to this called slippery now I'm going to add this to our place or we go to player you see here we have this physics material 2d so in assets I'm going to right click create physics material 2d and we'll call it slippery and I'm just gonna set friction equal to zero and also to balance just leave that serie I'm gonna click on player and go drag this into its material type and I'm also drag it into its box Collider material it's safe and that is going to make sure it doesn't stick to the wall which might seem Bakshi kind of want it stick to wall but we're going to write our own script to do this so there you go now we're not sticking and we can't jump from this wall to this wall yet so let's quickly go in for what I've done so I've set a bunch of walls and a pizza ground object which is cut with a layer of ground I then also on the player added office Lee the player movement script which takes in a bunch of different variables but it also issues in a rigid by 2d has a gravity scale of 5 and a collision detection of continuous sleeping mode never sleep and interpolate to interpolate and a freeze on this set axes the reason for these are just to make sure we're always checking when we're touching the walls and it just helps basically with laughing a greater scale five because I like how it looks when you fall down faster and you can't jump as high it kind of makes it feel more realistic than the slow jumping and grow slow falling so let's all let's take this with player and sister player and now we're gonna add in a new script and we call it wall jumpy jumpy slippy slippy you know what would do it in the player movement script and I'll go over to play movement script as we go through so let's double click this and open it up in Visual Studio code and as you can see here we have our unity use minty namespace and then we've got all our references here now we're gonna need to serve a few variables for the wall jump so I'm gonna create a new header I'm just gonna say header and call this wall jump he's gonna be our wall jump variables and underneath this we're gonna say we're gonna need to get a transform so we're gonna say public transform all notes like linear flow sorry we need to set a float off wall jump time I'm gonna set this equal to zero point 2f the reason for this is because well I explain when we get there but the wall jump time basically allows you so when you let go off the wall you're still able to jump for a certain period of time so if you don't do this it makes the game feel sluggish and the controls feel a bit crap so I'll show you how to use that later on so they also create a public float called wall slide speed and this how fast you want the character to slide down the wall I'm gonna say point free F I think it's a good speed and finally the public float called wall distance which I'm going to set to 0.5 F so how far wadester will have to be from the collider I say - why not do mine is equal from the climate collided to be able to be colliding with the wall to be able to grapple the wall so we're also going to set a ball off its wall sliding it's gonna be equal to false to test if we're grabbing the wall and then we're going to create a ray cast hit 2d and this one is just gonna be called wool check hit and this is basically gonna take in if lazy it's gonna be what we're using to check for touching the wall and what's gonna have a flow of just jump time which we're going to use later on to in combination with wall jump time to be able to make it less sluggish so our next thing we need to do is we need to go into our fixed update and I'd say below MX or below the movement we're going to say oh actually we'll even do it below derp it's touching ground stuff so I feel will do at the bottom there so we'll say here jump all jumpy we'll give it a nickname so now what we want to do is we want to say we want to use our if is facing right script to say if it's facing right we're gonna get the wool check hit and we're gonna set equal to physics 2d dot ray cast and we say transformed opposition so again the current players position will then gonna set a new vector to which we're going to say wall distance and then zero and to this basically so as you can see up here we've got the origin which is the position of the where we want the raycast to start shooting from without a direction so in which direction we want it to go and the distance which is wall distance so Wald essence were off see send it and 0.5 F then we want me follow distance which obviously will be wall distance and then we want the ground layer so the ground layer is obviously what we set and grounded up here and add a layer mask and I'll show you what that's set to in a second it's set to a laical ground which we've settled our Aldi object we want to be able to walk Apple to or jump from so we do that we don't also want to go in an else statement which says wall check hit again so if we're not facing right and we want to check if we're basically touching the wall on the left side so we're gonna say if physics 2d don't ray cut so wait I'm just gonna copy this whole bit here paste it in here let me zoom in a little hey yeah so now we're just gonna say is - wall distance because we're basically going we want to shoot the raycast in the opposite direction and that is all that's all we needed to change its that to be fair we probably could have created a variable call this once underneath and just set this to depend on if it's minus or plus Bostaph would have been easier but i've already committed to this so i'm gonna stick to it how what we want to do is we want to say if whoa if what if we'll check hit so if that's true so this will return true if it is touching a ground layer and we're gonna say and and it's not ground it's grounded so we're saying if it's not grounded which we are setting the it's grounded variable here and then we also want to say and MX is not equal to zero so we're actually pushing so we're not just letting go off it we're actually holding which direction we are in to the wall so we're actually it's like we're trying to grapple to the wall and you don't have to have this you can a fit so if you're just if you like slide off and you just fall into the wall you're just there but I do it so we have an input from our back so if this isn't serious so if someone's pressing a key into the wall then it's equal it's now touching the wall and then we're gonna say yes or not a scram it is wall sliding is equal to true we also then want to say jump time so degree so we're doing so what I said before is we don't we want to be able to if we let go of the wall we don't want him to just be like okay we can't jump now cuz if it'll make the controls feel sluggish you want to give them slight buffer time because sometimes users unknowingly let go of a certain key and then obviously or press the key flight in too late and you want to give them some sort of buffer time between that so we're going to get them off the point to 0.2 of a second so we can say time plus time plus wall jump time so that's where that comes into play and then we're gonna do an else if and all we're gonna check for here and I say if jump time is less than time time so if time if the time has passed and it's now further than that so if it's been zero point two two seconds we're going to say it's wall sliding it's equal to false it'll get reset false now we can say if it's wall sliding so this is the bit where we actually will slow our fall so so it's like all grappling onto the wall I would say it's wall sliding and then we'll say our B dot velocity is equal to a new vector2 and we're gonna say our b dot for lost filos my keyboardist hates me x and then to actually slow down the full speed you want to say math F clamp to clamp the wise speed and we're gonna say here it's obviously our B dot velocity dot y is the Y position the min will be wool slide speed so the speed we actually want it to do when YT crapping onto the wall and then for max we don't actually want a Mac so we're just gonna say float down max value and that is how we slow our character so we should be able to save now go back to our player I'm gonna click on it and check our saying so we have it will jump speed there everything is fine and as you see our ground layer is equal to ground which is set on all our ground objects up here so let's hit play let's move left now if I jump into this wall nothing happens so there's gotta be an error somewhere so let's go after look what's wrong we can actually put this into debug mode put it in here and we can see all our variables here so if we press play we'll see what's not working we want to see if it's water sliding works if we jump to the right you can see it's wall sliding never comes on so we need to figure out why that is so let's go back to our code and we're going to do sank so under rip physics raycast we want to say debug dot draw ray yep and then we basically want to say transform position and a direction which would obviously be just take another variable color so we're gonna say color dot blue and let's move this from here into here and then - if we're facing the other way it's safe let's go back let's hit play and let's see Y if that is the reason so disco show us some you need to make sure gets Moses check but you can see there there it is an air sight issue so you can see here it's actually not going on for the line - it's never the raycast is never actually touching the wall so we need to actually make that slightly bigger there's two ways we can do it we can either plus on our wallet or on our start position somehow or we can do it easy and a wall distance so if we go up to in our code here and we'd say audicence let's say with an extra one on the ends of five one and we hit play you should now see that it's slightly peeking out so if we jump here oh maybe not maybe if we go to oh I've gone CIRA 0.55 we'll say quite a lot for now baileygates and now you can see was slowly grappling down so it was just because of how it worked it's actually didn't see here we can't actually jump I'm trying to jump when I'm hooked onto warm we can't you jump so we can slide down the wall but we can't jump up the wall so let's now add in the wall jump let's go back and time off our debug I'm just gonna comment now in case we need it for a later date so I'm just gonna comment now and also comment this out so now up here to simply do this all we need to say is or it's sliding wall sliding and and input dot get buttoned down is equal to jump and it's as simple as that so that now allows us to jump if we're sliding as well we don't it because often it's not gonna be it's grounded we don't want to set its ground it's it's not grounded it's actually still in the air so we want to check fits it's separate so now if we go over to our wall we jump oh did I set this yep need to make sure that is five now so that should now be 5 5 5 which gives us enough distance to touch the wall ok and now you can see we can now jump and we made it to top yayyy so another thing you'll notice is when we jump from this to this and this it works really smooth but I'm gonna disable the time this this time so I'm just gonna put an else statement here I'm just gonna comment this out for a second so if we now go back to this and we hit play you guys won't notice anything really but from my point of view this feels a lot more sluggish so my controls are a lot harder to snow see I tried to jump there but promise I let go off the left analog sit so you can see it's a lot harder for me to control the play because if I let go off the right key but I want to jump left then I have to have to hold onto right key jump then press left instead of just pressing left straight away and jumping so it's like you have to jump before you move left whereas with the timer were able to start moving left and then jump off the wall that way which is more like how jumping it she let go after when and you push off with your feet so let's just undo what we did here so this is what the jump time does we can also apply this exact same jump time to our grounded so if we go here and we say it's touching ground and then we can set our jump time again here and we could say else if and we can do the exact same thing here which means if I play run slightly off the edge it has to split second to save itself and jump and this is what a lot of platformers don't do they think oh no it should be touching at all times of it's not touching then that's not their fault essentially like the player is just a play as well but you want to give your player chance to fix its mistakes or if you want to make it hard game then don't but basically I think everyone really agrees that not everyone sometimes you just run like kiss your own fill out so my own flap is like right on the edge so like when I go here you see I can't jump so if I'm moving really fast but I can see my player still touching it's not actually touching at this point here this point here so I can't jump up here in space but I can't jump and that's why we give a timer for this so if we're moving real fast and we get to here we should still be able to jump but we can't so that is it for this video I hope you enjoyed making a wall jumping mechanic it is super easy anyway guys don't forget to drink your water subscribe to the channel and like this video if you enjoyed it thanks watching and [Music]
Info
Channel: Muddy Wolf
Views: 13,777
Rating: undefined out of 5
Keywords: game dev, unity, game development, c#, devlog, indie dev, indie game dev, 2d, unity2d, devblog, wall jump, wall slide, easy
Id: adT3vSD-74Q
Channel Id: undefined
Length: 18min 3sec (1083 seconds)
Published: Thu Apr 16 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.