Code a Platformer Game | 5. Wall Jumping Done Right!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello fellow scratchers are you ready for some wall jumping action i'm griff patch and i'm so excited to get started today on part five of our classic platformer series because we are adding not your bog standard scratch wall jumping where players go bouncing right at walls no indeed we'll be doing things the right way with superb wall sliding enhanced jumping and epic parkour style jumping between walls this is going to make your project so fun to play you wait and see well what are we waiting for let's get scratching picking up from where we left off on episode 4 save your projects as a new copy and change the name there episode 5 cool we'll begin by upgrading our jumping scripts the first jumping mechanic we're going to address is the perpetual jumping scroll in the code to find the define controls up and down script so as i hold down the jump key without releasing it my player bounces around like a jack in the box this can be kind of fun but it doesn't offer us much control and makes jumping far less purposeful to improve this we'll make it so that each jump requires an individual press of the jump key we can accomplish this by adding a new variable naming it jumping for this sprite only then with an and block we keep the check for falling being less than three the number three here means we can jump as long as we've touched the ground within the last three frames in other words very recently indeed but now we'll also require the jumping variable to equal zero then as soon as we jump we set jumping to one this will stop us jumping again until the variable goes back to zero if we test that we do indeed get just one good jump and then nope like a chocolate frog we're done no more jumping for us and here's the trick we need to swap this if for an if else block make sure the change speed y is underneath and the jump key press is here and the if goes back in the then branch okay sweet because now this else only triggers when the jump key is released so we can set jumping back to zero all ready for a fresh new jump let's test this now so i hold the jump key we get a single jump and the jumping variable stays on one when we release the jump key the jumping variable goes back to zero and then we are free to jump again excellent this is a very nice feature to have in your game but we have a problem did you notice our jumping is no longer as powerful as it used to be we're not getting the same height we used to get this is because in the past we not only set speed y to jump force when the jump key was first pressed but also kept doing this until the falling was no longer less than three this gave us a few extra frames of full jumping power it was nice as it also let us change how high we jump by holding the jump key slightly longer well we still want this feature let's try it to put it back in to do this we'll separate the jump trigger this script from the actual jumping action pull out the set speed y block we'll use this in a moment in its place we'll set falling to three every jump will start on a consistent falling value now now bring in a new if block we'll check for when we've started a jump that is when jumping is greater than zero but we want a limit on how long we jump for so with an and block also check for jumping being less than six so we can change jumping by one and finally bring back the set speed y block to power the player upwards shall we give that a test you should see us getting the full jump power for up to six frames as long as we keep the jump key held down however by tapping the jump key we only get one or two frames of thrust and the jump is much shorter excellent work this is a lot more stable than the system we had before before we proceed let's be good coders and wrap this six value in a variable constant make a new variable naming it jump duration use it in place of this number six then find the when green flag clicked wow our scripts are really building up and set jump duration to six as always don't just try one value why not stuff in some over the top value like uh 20 and try it out here we go that is one crazy jump that's hilarious i can still do those smaller jumps but if i hold down whee it's more like i don't know a jetpack joyride or something how fun okay i'll stop playing set that back to six and hide those variables let's move on this next design choice may be a little bit more contentious in many scratch platformers when the player jumps releasing the walk key while in the air causes them to slow down and stop even in mid-flight it's not very true to life but more than this for our wall jumping to work well we need the player to be able to soar through the air to pull off those awesome stunts find with me the define controls left and right scripts at the bottom of the script here we are slowing down the player movement this is fine for when we are on the ground but not so fine when in the air scroll back up to where we check for key x being equal to zero that is when the left and right arrow keys are not pressed down drag in a new if right at the top check for when we are in the air if falling is greater than two so we are in the air and not pressing left and right so how do we stop ourselves slowing down so much i'm going to duplicate the slowing down script the set speed x to speed x multiplied by and bring it up here but remove resistance this has a value of 0.8 in my project i'm going to use a value much closer to one say 0.98 to slow the player down far less when in the air as always try some different values in here to have some fun finally we stop the script to prevent us slowing down further press the green flag i'm going to do the same jump releasing the run key oh wow yeah i'm not falling straight down anymore nope my player continues to sail onwards through the air until we land safely on the other side or slip off i guess we have to be a lot more careful with our jumps now i'm overestimating how much speed i need because i'm so used to just letting go of the key to slow down perhaps you may want to use a lower value than 0.98 after all this certainly is starting to make the game more of a challenge anyhow but as with any game the more you play it the more you get good at it and soon enough you've got it mastered right moving swiftly on wall sliding this is where the player can when in contact with a wall slowly slide down it we have to be careful to only activate this behavior at the right moment for example simply walking into a wall should not be enough instead we need to be in the air when the contact occurs and a slide shouldn't be triggered by just our head or feet touching a platform no we'll say it should be a central collision around the area of our player's waist lastly the slide must end when the player touches the ground or when they lose contact with the wall okay so we detect wall collisions in the clydex slope or wall script this seems a good place to begin make a new custom block for this naming it check can wall slide run without screen refresh just move it over here and drop the new block in as the first block in the clyde x script great so let's craft this new custom block we'll first rule out when we don't want a wall slide if falling is less than two then we're on the ground so no sliding is possible to record this fact make yet another new variable yeah i'm sorry about this it happens naming it wall slide for this sprite only so we're on the ground we set wall slide to zero then since we are not sliding stop this script next we want to check whether we are in the contact with a suitable wall for sliding down for this we'll use a new hitbox set costume to okay what do we have available look at the costumes hitbox version 2 was costume 21 and the very next one it's called hitbox wall slide that name gives it away doesn't it so look at how i've positioned it if i overlay it with the scratch cat you can see it's positioned to one side kind of like an arm reaching out to the right it's narrow so that it won't trigger for platforms above or below us but only when they're right in front you can move this around to find your sweet spot but this should do for right now back to the scripts switch the costume to hit box wall slide we just need to be careful though this hitbox requires us to set rotation style to left and right otherwise it will only detect walls to the right of us and that would be no good next check touching solid if the hitbox touched a wall in front of us then we now could test if touching is greater than zero but why not just set wall slide to the value of touching directly if there's a wall in front of us then wall slide will be set to 1 otherwise a 0 means no wall sliding nice with that done we should return the costume to hitbox version 2 and the rotation mode back to don't rotate okay click the green flag and let's run into a wall good the wall slide variable stays at zero next we jump aha the wall slide variable changes to a one that's great oh but wait it remains on one long after we've moved away from the wall well not to worry we'll address this in a second as we code up the wall sliding scripts back to the code and find the define controls up and down this time the last block in this script is handling gravity this will be a great place to implement the wall sliding as we want to slow down the player as they slide down a wall if wall slide is greater than zero then we are indeed walls sliding but we should check again in case as we saw the wall slide has ended use another check can wall slide block now to slow the player down while on the wall set speed y to speed y multiplied by 0.6 yet another value to play around with the closer to zero this value is the slower our player will slide down the wall testing so we jump into the first wall splat yes we have a very sticky face apparently brilliant and on this wall oh nice do you see how the wall slide finishes as we lose contact with the wall that's just what we were after but hold on there is an issue look what happens when i try to jump up a wall that i'm touching i can only manage a tiny jump that's no good the wall jump code is slowing down my upwards movement as well as my sliding down the simplest remedy is to wrap the set speed y in an if and check for speed y being less than zero that way it won't slow down any upwards movements and i should be free to jump up the wall still cool here we go jump up against the wall and now the upwards motion is restored and only the downward motion is slowed excellent so any more issues actually just one it's easy to show you if i make a little change to the level costumes we need one of the walls to be rotated to form a very steep incline okay now do you see how when i wall slide down this the player is being pushed away from the wall this is when i'm not pressing any keys do you know why find the define controls left and right scripts again so this is the code that lets us travel through the air without slowing down we just added this now the condition for this occurring is when we are not on the ground but it doesn't include when we are wall sliding the result is that we can now drift away from the wall too easily still easy to resolve add an and with the falling check on the left and now introduce a check for wall slide being less than one that ensures the script that keeps us moving in the air no longer applies when walls sliding just give it a test and ensure that we fixed that issue yep great the real joy of this platforming engine is that we are able to pull off these great moves even on uneven walls like this one it should mean we'll be able to design some really cool levels all we need to do now to complete this slide is to improve the visuals check out our costumes again scroll down to costume 19 it's named hang on 0-1 this one is an interesting costume as you can see it's designed to give the impression of holding or sliding down a surface to the right interestingly that this is the only costume to be facing to the left but this is very purposeful remember as we touch a wall we will be facing towards it so now switching to this costume makes it appear to face away from the wall we'll give this a go and you'll see what i mean find the define set costume script we deal with all the player costumes in here right away after the first set rotation style check if wall slide is greater than zero this means we are wall sliding so switch costumes to hang on zero one and then stop this script we are done okay let's go jump against the wall and look at this slide oh it's beautiful i'm loving how this looks it's worth noting if you are still using a cube for your player costume then you may not need a new costume for wall sliding at all simply leave the set costume scripts as they were and it will look just fine so my goodness this is it we are ready to add the final step the actual wall jump itself for this we need to find our jumping code again it's in the define controls up and down script the first if where we are jumping is the trigger for a jump now we will add another one for wall jumping duplicate the first if block and place it above the others so it's the first action in the keypress script now rather than triggering when we jump from touching a surface that's what the falling lesson 3 does we look for a wall slide being greater than zero yeah this triggers a jump while we are sliding down a wall we start by setting wall slide back to zero since we are jumping off the wall now we will have to do more than this though but let's just give it a test to see how it looks right now if i begin a wall slide and then jump great did you see that i was able to jump but i went straight up and also got turned around to face back to the wall once more a proper wall jump should have had me launching away from the wall surface not bounding right up it right so let's turn the player around by 180 degrees that will have them facing the correct way away from the wall then launch them away from the wall by setting speed x to um okay we'll take the current direction that's 90 for right and minus 90 for left and divide that by 90. that will give us either a 1 or a minus 1 depending which way they are facing then we are free to multiply that by our desired jump speed 7 will do i think oh yeah i am looking forward to this hit the green flag and woo hoo look at us go now jumping off a wall launches us across the level away from the wall and what's more if we land on the opposite wall we begin a new wall slide and can jump right back again that's really neat don't you agree so let's just check is there anything wrong with this okay yeah if we pull back towards the wall as we jump off it we can still change direction and land back on the same wall this lets us wall jump up and up a single wall of our game this is perhaps a gaming mechanic you actually quite enjoy in your game so don't feel bad if you want to keep it just like this but in our tutorial we are striving for excellence and as such we want to stop the player from pulling off this stunt to fix things we are going to force the player to jump further away from the wall before we allow them to turn back again make a new variable naming it long jump for this sprite only as soon as we've jumped away from the wall here set long jump to 12. so for a count of 12 game frames we'll stop the player changing their direction of travel find the define controls left and right scripts again stick in an if block right at the top and if long jump is greater than zero we want to prevent all changes of direction so start by changing long jump by -1 to count it backwards down to zero and then simply stop this script that will stop any further changes to the player's direction well that was a bit too easy right let's give it a test smash the green flag one last time so now i'm jumping off this left wall and i'm trying to pull back and no the game is having none of it i am completely unable to cheat my way back onto the original wall i jumped from but if i go with it and let myself land on the opposite wall then i'm quite free to wall jump back once more and again and again to jump up these walls in style yes this is just perfect for creating some epic level designs but just before we finish can we just be good coders and update our define reset and begin level script we have a few new variables that we should be resetting as the level begins set wall slide to zero set jumping to zero and set long jump to zero stuff them in after the rest of the set blocks great stuff so with that our episode is done time to start playing around with your level designs try to build out a fun park or course to jump your way across i simply cannot wait to see what you guys can put together i've included a link under the video to the scratch studio where you can submit your projects from episode 5. it's so fun looking through them and i hope to include a few in the next video to show off what you've been achieving they're already blowing my mind so go to town and let me see what you can do so awesome along with that we've also got some really exciting episodes ahead of us the fact we can now jump up and climb so high it's just begging us to add up and down scene changes but we also need those moving platforms dead excited for that and don't forget collectibles enemies danger sprites so much to look forward to okay well i do hope you've enjoyed watching today do smash the like button and don't forget to subscribe to my channel to catch the latest videos if you're a loyal fan or an educator then please consider joining the channel membership which helps support the channel further you can also get extra perks like prioritized comments channel emoji early access to videos and even the finished tutorial projects themselves how about that so thank you for watching have a great week ahead and scratch on guys [Music]
Info
Channel: griffpatch
Views: 69,927
Rating: undefined out of 5
Keywords: scratch coding, scratch programming, scratch game, scratch 3, griffpatch, griffpatcho, scratch wall jump, walljump, wall kick, wall jumping, block coding
Id: lG9R-rc1z28
Channel Id: undefined
Length: 22min 20sec (1340 seconds)
Published: Mon Oct 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.