gamemaker studio 2 : platformer game apply gravity the right way

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi i'm neither ansari and in this video i'm going to show you how you can apply gravity in your platformer game or on any other game that you want a gravity so without any further ado let's begin so you remember from last video we did create two functions one of them for input check and one of them for movement okay so no i want to create another function for applying gravity okay so it will be more readable for other programmers let's say you are in a group of programmer that you are working on one project and you should write your code like other programmer can read it or if you are working on a project and you publish it and after one or two years you want to change something or improve it and you should be able to read your code after two years okay so it shouldn't be a mess in this series of videos i want to show you how you can do that so your code will be more readable for you and other programmers so we did create this function so let's just go to a script in scr player if you didn't watch my videos about platformer in game maker just watch that playlist in that playlist i did show you how you can import a sprite sheet how you can import a sprite in when it's in separate images to game maker to have animation and i did show you how you can you have movement and input check in your game for platforming game in the right way note that we want to have this applied gravity let's just copy this up like gravity and create that function in our scr player so all the function of our player should be in this scr player so we say function apply gravity and now we have this function and with that whenever any other programmer or you after one or two year check your uh project you know what this input check is doing we know you know what this movement is doing we know what this app like gravity is doing and you can see it's just tree line of code in a step and you can write read it fast and improve it if you want really fast so for applying gravity let's just go to here down and let's add graph code to our project let me again run it to see what we've done so far if we go right or left we have these and you can see that we can't go through any obstacle so we want to apply gravity toward player first of all for applying gravity gravity you know that in create event we create a vsp for vertical speed for over gravity so what is the speed of falling to the ground okay so we use this vsp to apply gravity okay in this we want to apply this ysb to y of our object player okay so our object player need to have gravity so we say y equal y plus we speed okay so with that if we run our game because vsp is zero nothing will happen why because we didn't set the wii speed so what we should do let's just create a gravity variable in here let's call it grv for gravity and let's give it something like point one point five okay so this is for gravity and we know let's just go to uh our apply gravity function and in here change the v speed to be to be equal to v speed plus gravity okay gravity that we set in create event and now let's just run it again our player should fall and you can see it let me go to room put it upper in here for you to see it clearly and you can see the gravity is there and it will increase its speed at each frame because it's falling and it's uh it's how it worked in real life so let's just close it and whenever this player reached the ground or this tree or this wall it should uh stop there so know that we have this we should stop at the ground or any obstacle it should stay on it you remember from last video we create an obg obstacle and we make it apparent of all of the obstacle like ground like tree like wall to for us to uh check it fast so if we in future at any other obstacles if it was the child of the obg obstacle the our code will detect that and won't go through that okay so that's how you shoot on that if you didn't watch my last video about these obg obstacles just watch it it's too much important so now that we have this let me show you how you can stop your um vsp when it's reach a ground tree or any obstacles for that like last video we say if place meeting in this case we should say x of our player and y plus we speed i will explain why i'm doing this but for no just have this vsp and we should check for obg obstacle okay what this line of code let's say this is our player okay and it's falling down and we check if the position of the y position of player plus the next frame y position that is v plus vsp is meeting or will be collisioned with or obg obsecule it should make the vsp 0 or it should stop at there okay so we do it like this if the player that has x and y position in our room plus the v speed that in this case it may be a value of 5 or 10 or whatever it check it and if it's reached the obg obstacle just change the v speed or vertical speed to zero okay so now let's just run it to see what effect it has in our game and you can see that our player stay on our ground and if we go to a room and put it above this tree in here and run it again it will stay at the top of this and again if you put it on top of this um wall in here again it should stay at the top of that wall and you can see it yeah the gravity is working fine but there is a problem in here and you if we zoom it a little bit uh we can find out that um this player is not really at the ground it's it's some pixel above the ground what we should do about that let me show you uh in this if function we should say while like last video if it's not meeting at what one pixel distance between or player and or ground or any obstacle so we say place meeting in this case again we should say x and y plus sign of or vspd with obstacle obg obstacle while it's not meeting that just change the y position just by one pixel y plus sign off vspd so let me explain why this what this sign is so if you watched my last video you know that you know that you know but let me explain it one more time so the vsp it's maybe 10 it may be 15 if it may be 20. but the sign of that is just returning if it's positive or negative if the value of vsp is positive if it's 10 20 or whatever it will give us one and if it's less than zero it's like minus 10 minus 20 it will give us minus one and we use that to go further down until we reach our ground at one pixel per second okay so with that we can run it and you can see everything is look perfect and now we have our gravity and again let's just collapse all of these go here and you can see we just realigned we have input input check we have movement and we have gravity and if any other programmer look at your code or if you look at your code in next few years in the future years um you can read it more easily and if you want to improve it you can improve it fast so in the next video i will show you how you can have jump and after that how you can have a state machine to have all of those cool animation to be more readable in the right way thank you very much for watching if you want to help me please hit that like button it helps me a lot thank you very much bye you
Info
Channel: navidrct
Views: 44
Rating: undefined out of 5
Keywords: gravity, physics, beginner gamemaker, gamemaker, gamemaker studio beginner, gamemaker studio, gravity in gamemaker, gravity in gamemaker studio, gravity in gamemaker studio beginner, gamemaker beginner
Id: LmbHqoVpJQU
Channel Id: undefined
Length: 8min 26sec (506 seconds)
Published: Thu Dec 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.