GameMaker Studio 2 - Jumping

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right hello this time we're going to have a little look at jumping mechanics in a maker studio too so let's get to it I've done a little bit of setup this time I have two sprites one for the player one for the wall and then two objects corresponding as well the sprites are simply just squares as you can see they're in the corners and the objects here the player object has a little bit of code in the create event I've set a move speed variable for moving left and right and then there's just some code in here to allow us to move left and right as well some inputs and then condition check to bump into walls to the side of you and in the wall object I've got no curve at all it's simply but it's sprite designed so let's go into the room let's put these objects into the room so here's our player and we'll put some walls in and let's run the game and see where we're at at as a starting point and then see what we need to do to get some jumping going on here so here we are we're moving left and right we're not falling down like we want to and there's no drop of functionality we've not built that yet so let's do that so the first thing we're going to do is we're going to create a forest that pulls us down to the ground we're never right in there so we can walk along the floor rather than floating in the air like this so let's go into our player object and we're gonna create a couple more variables firstly in the creek in the create event so we'll title this with first-come movements and there's gonna be three variables I'm going to create two first one's going to be gravity I'll call it geography that's gonna be the force that's pulling us down to the ground give it a value of nine point three four now next we're gonna create jump Heights which is fairly self implied it's gonna be so high we can jump give that value of seven and then the last ones we're gonna call it V SPD for vertical speed start off at zero and this value is gonna what's it going to do this value is going to keep track of our current vertical force or current vertical speed if you will and whether we move it up or down and we're going to be applying this force to our y coordinate every frame so let's start doing some coding in here then so the first thing we're gonna do is we're gonna let's just title this in here as well for vertical movement just to separate our blocks of code a little bit and what we're gonna pin here the first thing we want to do is help ourselves pull to the grounds mmm so we need gravity to pull us down so we'll say vertical speed plus equals grv so we're adding gravity now force of gravity to a current vertical speed and then at the end of our car is marched like this movement up here we'll say y plus equals V SPD so after we've done all of our calculations we're gonna actually apply it to our y coordinate or Y position so if we've run this code now it's gonna pull us straight down but we're gonna go straight through the wall as well it's not going to stop just yet we need to actually code in that collision Beeman is quite simple to do so let's just give this a little a little comment for a gravity and then we'll do a collision check to the floor so we're gonna use the F place meeting function for this if place meet say X because for not if we're changing X will be looking left to right we don't do that below up-and-downs we're changing our y coordinate and x + v SPD the reason we check that is because that's how much we're going to be moving each frame so we want to make sure that we're not going to move into a wall so we're checking for the wall before we've actually done the movement let's give this little comment as well for convinced a movie ok sir and then we're looking for a walk so f place meet saying vertical speed below us the wall then we want to stop moving we don't want to move through the wall so V s BD equals zero however if we are five pixels away from the ground and we stop our vertical speed right there we're going to be flowing five pixels above the floor so what we're gonna do in that case is if this returns true if it's found a a wall V SPT value below us or above us we're going to move as close as we can to the wall without moving through it so we want to like line up against it nice and smooth we're going to say while this time while not placed meeting so for as long as there's a lot of wall at the position s y plus sign be s P D which will return plus one if this is positive or minus one if this is negative and per still check it for the wall so while there's not a wall born pixel above or below s let's move our Y position by one thinks of buffer blowers sign the VSP D again and then once we've aligned right up beside the wall we'll set our effort must be to zero so that this doesn't then register the wall so this code here is going to allow us an L walk along the ground there we go lovely nice and smooth and we can fall off the edge here and then fall down here now we're stopped so let's get some jumping mechanics in here so we can free ourselves okay so last thing we want to do this creates our jumping action so in order to jump we need two things to be true we need to be on the ground and we need to be pressing the jump button so that's a sign a button for the jump button in our inputs at the top ki-jung equals keyboard check please the spacebar for this which is VK space and then go back down actually one more thing so while we're up here we have also set a variable that checks whether we're on the ground or not so we'll call it is grounded not equal to place meeting X y plus 101 it's worth noting that plus one pixel on your y-coordinate is 1 pixel below you so I should go down your y-values increase and as you go up your Y value decreases which means whenever we apply our jump Heights but every ritual never we try and jump we're gonna take the job high value away from our y-coordinate or from the vertical speed or however we implement sir so this will return true if we're on the ground and it will return false any other time so now we've got our two condition set up we can just simply check if it's grounded so if we're on the grounds and ki-jung we're pressing the jump button vertical spaced - our jump height - equals our jump Heights and that means whenever we come to here it's gonna jump and you're gonna see this is gonna be really smooth jump because when we're in the air the gravity is gonna pull us down nice and gradually we'll be at zero speed at the top and then it'll pull this down again just like that lovely John okay so there's a couple ways we can alter this to try and improve it one issue with this kind of jumping is that it doesn't feel great whenever you're running off an edge like this and you're trying to jump there'll be a lot of times when you try and jump and you've just run off the edge and because you're no longer on the ground you just fall it doesn't feel good for the player for that to happen a common way to fix that is to recreate a small buffer of time after you've left the grounds that you can still jump just so that the player feels more in control of their character so let's look at one way of how we can achieve the distal buffer to allow us to jump once we run that ledge so to do this what I'm gonna do I'm going to create another variable in here in our create event called can jump you're going to set that equal to zero for now and the way we're going to use this is whenever we're on the ground we'll set it to a value for example 10 and then whenever we're not on the grounds we're gonna decrease it by 1 each frame and as long as can jump is more than 0 will allow the player to jump so we'll see how we can code that in now so we're gonna do above this jumping action so like I said before if we're on the ground we're gonna set a can jump variable to a value like 10 sir it is grounded can Jung equals 10 and while we're not on the ground elves can jump will decrease by 1 each frame - equals 1 / this aside brackets as well just to make it easier to read perhaps so if we're on the ground and we're setting it to this if we're not on the ground we're decreased in that value until he reaches zero this means that instead of checking if we're on the ground on we try and jump we just need to check can jump is more than zero can jump is more than zero okay so let's run this and see how that's working Oh error okay interesting Han Jung magnetic was one right so you see in the top here I've just drawn the value of can jump something keep track of it you see whenever we're in the air okay so you can see that when I Frank jumps now I've gone quite high reason for that is because when I've jumped can drop is decreasing by one each frame which means for ten frames it's applying that jump height over and over again so I can fix this two ways and how the first way to do that to fix it is to go into your jump action and whenever you've jumped set can jump equal to zero so it only occurs once so that will totally fix that we don't have to do anything else but we're going to anyway what we can also do is change our key jump the keyboard check pressed which means that it will only return true one frame when we press down the spacebar and then it will be false until we've let go and then press the spacebar again so you have to manually press John each time you want to jump it wouldn't just helps you up and down and up and down okay so now let's try it okay so jumping you can see whenever we're in the air it's going down but let's try one off the edge see if we can joke we can so there's a small offer that we've allowed that lets us sort of run off the air and jump you can change the amount of grace that you're given the player by just decreasing the value that can jump is set to one thing you may notice is that whenever we're running off the edge it's the jump is kind of weak it's not a very it's not as powerful as this jump here the reason that happens is because whenever we are falling off the edge here grab here start to pull us down to the ground which means that whenever we're trying to jump it has to counter that gravity has already built up and then whatever is remaining is what will push us upwards if we want to just allow the player to do a normal full jump it's quite simple to face all you have to do is whenever we jump we will reset the vertical speed to zero and then that will allow us have our full jump so we'll see that in action here and these are fairly these are really simple fixes it's just nice to see how each bit of code actually works in Europe in your game so there we go and you can see there the jump is much much stronger it feels a lot better now you can change all these values depending on your game of course and depending on how you want to feel the last thing that we're going to do is we're going to see how we can implement a double jump mechanic it's quite common in video games obviously so how we're going to do this is we're going to create another variable just like an jump except we're gonna call it can double jump and we're gonna set can double jump to true whenever we've jumped and then we double don't that will be false and also if we're on the grounds it will be false as well okay so let's create the variable in the create event can double jump equals zero we're gonna we can use 0 and 1 for true and false or we can actually write false and true so that's just let's use true and false for this why not so let's see when everyone on the ground so here when on the ground we we can't have a jump we want to just do on our normal Java for on the ground so can double jump that's false once we've jumped once here you won't can double jump to be true so again we're just at am hand double jump close tree and then we'll do a condition if can double jump and he jump so if you press jump again and you can double jump and you will basically run despotic and vspeed equals 0 and the SPD - equal job back is just jumping here F can double jump and he jump the VSP D equals 0 is you're gonna reset back and you don't have to but I will here the SPD - equals jump Heights one really important thing here is that oops let's see so this is our double jump one more thing we need to do is whenever we've double jump we're gonna set can double jump to false again because otherwise we're gonna be jumping all the time because false okay so one important thing to know here is that this block of code for our double jump it needs to go above are no more jump the reason why is every jump here and we've set double jump equals to true in this frame because we fresh jump key jump is true and also we've just that can double jump to true so now here this is going to be true every single time we jump in the same frame so it's instantly gonna call this code we're just going to set can double down to false so it's gonna be redundant really so I'm just gonna hit control eggs and cut and up here I'll paste it there and we'll just label it so this is our normal jump here and this is our double jump so let's go into our code and see that in action lovely look at that fantastic feels pretty good feels pretty good alright well I hope that's been useful
Info
Channel: Caz Creates
Views: 5,570
Rating: undefined out of 5
Keywords: GameMaker Studio 2, gml, gms2, jumping, tutorial, programming, help, beginner, creatorCaz, platformer, game dev
Id: bhAewBxIUv4
Channel Id: undefined
Length: 21min 7sec (1267 seconds)
Published: Mon Mar 04 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.