How To Dash & Double Jump - Godot 4 2D Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys today we're going to write a little code to make our character controller support dashing and double jumping let's go all right so we've got our player here in the scene so we'll start by heading over to him and going into the player script now I've got some stuff in here that you might not have I've added a couple of animations and stuff but that doesn't really pertain to what we're doing here today so to start with we want to go ahead and just Implement our double jump so to do that we're going to need a variable called jump count and we'll set that to zero next we want to come down to where we have our jump button being pressed and we're just going to paste this twice Below on our first jump which was this one right here in the middle we want to set jump count equals to one so this is our player has just jumped we're going to set jump count equal to one on our second jump which is this one right here change jump count equal to two and since we're already going to be in the air we can take away is on floor and make sure that this just fires when jump count is equal to one and then when we're back on the floor we want to say if we're not pressing the jump button and we're on the floor set jump count equal to zero and we don't want to apply any velocity to the player so we can go ahead and save and let's test that out here we can jump and and double jump perfect can we triple jump we cannot triple jump all right so that's working now let's head back up to the top and we're going to create a couple of new variables here for our Dash so let's go ahead and create one for dash speed we'll set that to four and we'll also set up a flag for is Dashing and we'll just default that to false we can go ahead and save now we're going to come right down here after our direction is set and we want to see if our dash button has been pressed so first things first if you don't have a dash control already set up in your input map go up to your project settings and under input map you can set up Dash uh to do that you just type Dash hit add and that will add it down to the bottom you can hit this little plus sign and it'll let you hit whatever key you want for this one I'm doing it with the right right Mouse button um but it can be any button that you choose so we want to detect when that's being pressed so we'll say if input is action just pressed and we're looking for dash then if we're not dashing and we have a direction start Dash so our start Dash function isn't set up yet so we'll come down here to to the bottom and under move and slide we can go ahead and set up our start Dash so for here we're going to want to set is Dashing equal to true cuz we are dashing then we need a timer so over here I've got a dash timer set up and it's at0 4 seconds with a one shot set so we can just drag this Dash timer right in and we will connect it on timeout to a function called stop Dash right after we connect it we'll go ahead and start our timer so we obviously need a function then called stop Dash where we will set is Dashing equal to false and so what this is going to do is when we hit our dash button it's going to trigger this function set is dashing to true so we know we're dashing and then start our timer our timer is going to go for 4 seconds and it's only going to go one time so it won't Loop forever uh that's what this one shot is once that's done it's going to trigger this stop Dash function and that's going to make is Dashing equal to false so let's go ahead and apply is Dashing to our movement so back up here under where our direction is set you can see here where setting our x velocity is equal to Direction time speed so what we want to do here is we want to say if is Dashing then we want to do Direction times speed times dash speed else so if we're not dashing then we can just do regular Direction times speed for our velocity let's see what we get here so I'm running and then I'm going to hit my right Mouse button and I dash for 4 seconds all right now we can make this look a little bit cooler with some particles so I've gone ahead and set up some Dash particles right here and I'll just walk through the settings with you real quick so we've turned off emitting because we don't want our particles emitting from the back of our character just by default I've set the amount to 50 lifetime to 0.5 so that's the amount of time that the particles are going to stay on the screen it's half a second and we want a little bit of Randomness so I've set this to0 46 for the draw order I'm doing lifetime instead of index that just sort of Smooths out how the particles will come about and then for the texture I'm just using this white hexagon that I made in Microsoft Paint you could really use any sort of shape you want to to sort of determine how your dash particles are going to look after that we've set the emission shape to rectangle and for the wct extents I put y to 40 and what that's going to do is that's going to stretch out the height of the rectangle to about the height of my character for Gravity I've changed the x value to minus 2000 pixels and that's going to make it the dash particles go to the left of my character and under linear acceleration we've changed uh the maximum acceleration to 66.4 2 anywhere around there works and then I've gone to create this curve where I'm starting out really high up top and then by the end evening out at zero so this will make the particles come out of the character really quickly and then they will slow down slowly as they go along so we've got the minim max value at minus 200 200 that could be 0er to one it's however you want to set up your range I just find this convenient so for the scale here we've got a Min scale of zero which um we what we want to do is we want the particles to come out of our character large and then sort of scale down as they get further away so our Min scale amount is zero and our Max scale amount is 0.2 and you may have to adjust this depending on what size texture you use uh mine's 256x 256 so 0 2 is about 20% of the total size that tends to look good here uh and then for our scale amount curve we're starting off at one up top and then about 75% of the way through uh we're going to go all the way down to zero and like I said that's going to start the particles out at 20% of their original size and then as they get further away from our player they are going to disappear by shrinking for our color ramp here I've Set uh green going into white and that's just to match the colors of My Level this could really be any kind of gradient that you want to just sort of give it some flavor give it a Vibe and then for the ordering I've set the Z index to one and this is because my player is actually on Z index 2 this may be slightly different for you but what we want is we want our zindex of our Dash particles to actually be lower than the zindex for the player and that way the particles will show up behind the player first thing we want to do is we want to make sure that depending on the direction of the player we are flipping our gravity for the dash particles so that way if we run left they the particles will sort of fly out to the right behind the player and if we run right the particles will fly out to the left behind the player so to that I've got this um conditional here velocity X is greater than zero and I'm using this to flip my Sprite depending on which way he's he's running and so we'll just sort of piggyback on that and say uh- particles. gravity dox equals 2000 and or actually equals negative -2000 and this is uh set up because when we did our Dash particle gravity over here we set that to minus 2000 and so we're just going to keep the same sort of value um the opposite of that being positive of 2,000 if velocity dox is less than zero so the next thing we want to do is we want to apply our Dash particle emitter um inside our start and stop Dash functions so we can have Dash particles. emitting equals true and then we'll just copy that and bring this down here to stop Dash where we will turn off the particles and let's see what that looks like all right running and there we go I have my Dash particles and what's cool about this the particles work in World space so as I'm going along and I'm jumping um you can see they kind of curve and if this isn't happening for you then in your particles you can make sure you have local coordinates unchecked if this is on and we try to do it you'll see the particles always just come out in a straight line behind my character they don't really have that Dynamic sort of feel to them and so we want we want local coordinates to be off and that will give us this nice sort of like curve that follows the direction we're jumping so if we combine these um we can actually do some cool movements so we can sort of zip up to the top you know with our double jump and our Dash and uh we can sort of hop all around the level and you know you can play with the parameters to make it work however you'd like but I feel like this is a this is a pretty good mix of double jump and dash that sort of gives me the result that I was looking for um so that's it guys that's how you double jump and dash and put a cool little particle effect behind your character uh using goodto 4 thanks for watching
Info
Channel: swydev
Views: 551
Rating: undefined out of 5
Keywords: gamedev, game dev, godot, godot 4, godot tutorial, make video games, game dev tutorial
Id: NhHpDcpY8ok
Channel Id: undefined
Length: 10min 38sec (638 seconds)
Published: Fri Jun 28 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.