Wall Jumps in Unity 2D

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey this is crony baby and in this video we're gonna learn how to make world ups in nearly 2 B so we are going to start by making something like a wall slide right and then we'll end it by making a jump so with that said let's get started so this is a normal scene with a player where we have a movement script that helps with our movement and jump let's just open that end but this script you can just download it from the link in the description or you download the whole thing from the link in the description so that you can follow along pretty easily so now let me just give you a little bit of information the information is these four these four values help us move the player and flip the player as well as control the animation of our player when it is moving it is moving is true anyway so don't play otherwise animation play moving animation play these four lines of code help us check if you are grounded or not and if you are and you press the jump key that means you can jump all true then you will jump which I want to jump for this is the reference to the animator and the rigidbody of our game of our player basically here in the input in the x-direction input we put the horizontal controls and when we press the jump key then can jump will be true in the check ball we check if we are grounded or not by creating a box on our ground checkpoint position and with a ground check size and this basically this ground check position and size this box basically checks for our ground layer and if we do touch the ground layer ground it will be true and we can jump and this here if we just press the left or right key or a or D key meaning if in X Direction input is not equal to zero then we can play the movement control movement animation otherwise we won't play the movement animation this sign of course just helps us move the player as you can see and this helps us flip the player this functions it helps us flip the player this function helps us jump when we press the jump key and we are grounded we can jump and these to help us play the animation so if according to this is movement boolean this is movement or this movement animation gets played and according to this grounded boolean this jump animation gets played and this just helps us create the box on our sins so if I just hit if I just click here as you can see down here this has helped us create a box and this box helps us see if we are grounded or not now let's hit play so as you can see I can move the movement animation place I can jump this jump animation place does not look that good but you get the point so now let's make our wall slide so up here listen let me just copy this header put it down here and it said it is jumping right is false like I'm sliding now here we need first of all say serialize filled float and we need a wall slide speed most likely we can just set it up as 0 and then we need exactly these small things or literally the same thing as these guys alright so if I want to I can just copy and paste it here and just change the names to wall and we literally are going to do that so let's it's Eli's field night layer mask and let's call it wall a wall layer another let's say slice field transform wall check point and lastly say lies field vector to me and war check size now we also need these two things one will check if you're grounded and one will check if you're touching the wall and the other will check if you are sliding on the wall or not so let's do that private boolean instance wall and another private boolean is sliding so now let's just set up our it's touching wall so in our Czech world function let's just go here literally you can just copy and that's it and just change the variable here and you can just do that but I'm going to show you so yes touching wall is equal to physics 2d don't overlap box with the same thing and now instead of ground check point for our vector to point we'll use one check point dot position instead of ground checks I will use wall check size for the angle zero and instead of ground layer we're gonna use a wall layer so once we do that let's just hit save the latter unity and now you can see other things here won't slide which layer we will we are going to check mine what is T what do you call this wall checkpoint and the size of our wall check box so for the one we'll check layer we're gonna use wall right the reason why we're using wall and ground is because for the ground for all of these grounds that I have made in my time map I have set them all to ground and for the wall I have set them to walk you can just add a layer whatever layer you want and just press ENTER and you'll have that layer and you can just set it up as that layer like this so let me just put it back to work all right in here we are going to check for the wall layer and the thing that is going to check for the wall layer is my wall checkpoint I have already put it in front just put it in front of your character alright so I've already put it in front and let's just drag and drop it here then for our won't exercise for now let's just keep it both of them as one and now the reason why we can't see anything here is because we have not done this we have to set the gizmos for this guy as well so let's just do that let me just for that let me just copy this because it's the same thing as I said and let's just do that so or wall for wall check for foreground all right for ground check for lawn check and know this is blue let's make this color color of this gizmo as red red and now instead of ground check point position let's have wall check point position and instead of ground check size let's have one sixth sense now after we do that let me just hit save and as you can see now we have this box now let's set that box up if I put like 0.05 just looks perfect right now this guy will check if we are touching the wall or not so let me just hit play right now it's touching more is false and because we are touching the ground is ground it is true so if I go here and touch the wall now it's touching is also true which is great now let's make a function called down here just below our jump let's make a function called wall slide let's make a function called wall slide and now for us to both side we need to like match some amount condition which is if we are touching the wall and we are not grounded and we are not grounded and we need one more thing which is called and RB don't velocity on the y-axis is less than sorry less than zero so meaning if we are touching the wall and we are not touching the ground and we have started falling down while we are touching the wall then we will do some more sliding so here right is wall sliding is equal to true else is one slide it will be equal to false now for wall sliding what we need to do is if we are wall sliding that means is wall sliding is true change the velocity of the player on the y-axis that means like this RB don't velocity dot naught dot is equal to new vector2 and we won't do anything on the x-axis so RB don't velocity dot X will go now and change anything on the x-axis but on the y-axis its velocity will be our wall slide speed so now once we do that once we do that if we have to first of all we have to just make a reference to this so let's go up in our fixedupdate because everything related to physics will be kept in will be and should be kept in the fixed object so more sliding all right now once we have made the reference let's just hit save go back and now let me just put it normal now one side I'm not gonna put anything here I'm gonna tell you why but if you want to put something put a value between zero and one all right I think minus minus value will also work but don't put too much otherwise it will start sliding too fast right don't put a value above one cuz that will literally make you go up instead of down so let me just show you if I put and that's the most wall sliding most lowest wall slide you can ever have like that is too slow if I put it as 0 it's actually fairly good right if I put it as let's say I put it as - yes we will literally start going up yeah so put a value from basically 1 - about - how much how much you want if you take it to like - - - what's gonna happen is it's gonna actually start like going down even faster as you can see it is one sliding but it's way more faster right so the sweet spot for me is actually zero so I'm just gonna keep it as zero I just have a good Oh slide so now you know what sliding is done let's add some wall jump let me just copy this header for a wall jumping now first of all we want a jump or let's say wall jump force so cell ice field float walls on force then we need the direction on which we will jump let's say you are facing left right now you're facing right let's say and you just touch the wall when you hit the jump key after you are on the wall it should literally move backwards like this so we should have a negative position right we need a wall basically we need a wall jump direction basically so right float wall jump direction so a volume direction and then this we need to make our player jump at a certain angle alright this value we need to make because we want the player to jump at a certain direction so for that let's say civilize field float wall jump angle oh yeah by the way it should not be a float it should be a reactor - according to this vector - will jump at a certain angle 9 according to this will jump at a certain direction and according to this will jump at a certain force so for the force let me just set it up as 18 for the jump direction because we are facing right we need to jump in the opposite side so let's keep it as minus 1 and we jump angle will set it up in the inspector so now first of all whenever I am moving or whenever I'm flipping this value should also change so in our flip function up here just right wall jump direction and multiply it by minus 1 minus 1 so now every time I flip what's gonna happen is whenever I'm on the right side it will be minus 1 right whenever I flip on the left side what's gonna happen is this value will turn into plus 1 that means if I am facing left and I and I climb the wall and I do a wall jump that means I'll jump on the right side so that is why we need the walls of direction now let me just do a wall slide below the small slide you need another function called we need another function called wall jump so here what we need to do is if we are wall sliding we are either won't sliding or even if we are just touching the wall then we can all jump alright so if your wall sliding or and put it inside another bracket on it so if it was sliding or let's say you're even just touching the wall then you are able to jump but you should also have to press the space key that means can jump should also be true so if you are either touching the wall either touching the wall or just lying on the wall and you press the jump key now you know here no it's just so area when I'm 50 junkie can jump will be true then you can walk jump so you can jump all right and look after you won't jump your can jump should be false or you can jump will be informed so now what we need to do is we have to add force to our object making do a water so for that right are be don't not velocity and foremost not angular drag add force so are we gonna enforce a new vector to new vector 2 2 and then on the x-axis we need to write volume Force which will multiply by our walljump direction and then we'll multiply it by our wall jump angle on the x-axis because this is the x-axis for the y-axis do the same thing right wall jump force and multiply it by wall jump angle but we won't put the direction because we don't have anything to do with direction on the y-axis or the angle only y-axis by the way so once we do that here we need to specify what kind of a force it will be so for smooth 2d don't impose so basically let me just show you we have put a force on this angle basically with some amount of force on the direction with an angle on the x-axis and some amount of force on the angle of y-axis so once we do this our volume should work so let me just make a reference to it in our fix update function and once we do that if I go back and know the walljump is good the direction actually we should not be seen in the inspector but for now you can know what's going on here as well for our jump angle in the x-axis now to have a good jump what you should have is this should be more height than the width of your jump that means on the x-axis put one and the y axis put two three now when I hit play first of all look at this all right because we are facing on the right it's minus one when I start facing left it'll be plus one and that just helps me do a wall jump on the right side so after that let me just do this okay but as you can see when I press the jump key it just goes straight up now the reason is because of this thing this should only affect the angle of our jump this should not affect the force of our jump but what's happening here is that because X is 1 so the force on the x-axis is just 18 but the force on y-axis is 18 x 3 then that is affecting our jump as well this would only affect the angle for jump so to help us with that we just need to normalize this vector so in our start function just right wall jump angle dot normal line once we do that this value just get normalized meaning right now it's 1 and 3 right but when I play what's gonna happen is it's gonna be zero point something something and 0.94 this in turn will not affect our force so now I'm also lighting again and when I jumped it is jumping actually it is jumping the reason why you can't see a good jump is but I don't know how to explain that but it's the reason because why is that where did it go because of this this line of course literally just dampens your and force beside force value just gets dampened by where did it go this line of code so to stop that just what we need to do for now is that if we are grounded only then the player will move this brings another problem because once we start jumping we won't be able to move for now our wall jump problem will be solved so let's go back hit play and if I know as you can see just press the jump key and it works perfectly right but as you can see you can also do something like this oh yeah it works but you cannot move the player when you're jumping now I feel fixed and mostly everything will be done so let's fix that right LCF else if we are on the air so when we're on the ground we move now when we're on the air that means we are not grounded and we are not touching or just sliding on the wall so right so we are not sliding so if we're on the air and we are not sliding as well as we're trying to move that means our X directional input is not equal to zero right so when we're grounded not grounded sorry and we're not all sliding that means we're on the air my probably jumping and we try to move our player then we'll move our player but will not add velocity but we'll add at force so we need a RB dot and force which will have a new vector2 to move on the air we don't we should not use our move scale we will use something else which on the top let's call it air move speed right so sterilized field float air speed weights let's set it up and again I guess 20 or maybe 30 because we need a lot more this because we are literally fighting inertia and we we need a lot more need a lot more value let's go alright so here we'll just write our bead on and force is equal to new vector2 and only x-axis we are going to write air movement speed which we will multiply by our x-direction on input and only y-axis we don't need to do anything so just put it 0 per our force more 2d we don't need to do anything because it's a constant force so we don't need to write anything and after doing this it's gonna happen is let's go back here and the force is hundred now if I hit play I will be able to move the player but not that much actually I do have a little bit of control if I just do this as you can see a little bit of control but not that much of a big control as you can see it's not such a big control only a little bit of control when we are actually want to think so now to bring the control back what we should do is the reason why the control is not there is because the our baton velocity meaning the rigid bodies velocity on the x axis is now greater than the movement speed and in this when you add a force you have to go against momentum as well and because of the momentum you cannot control it properly this cannot actually overcome momentum quickly enough so that you can control the player so to do that basically just write if and write in math F dot absolute and inside the bracket right RB don't the velocity don't X is now greater than the movement speed if your rigid bodies velocity no matter if it is positive or negative that's what we used absolute that gives us a positive value no matter what is greater than the movement speed then this will take off all right we will use velocity to change the movement of our player all right so what will happen is when you're just jumping and trying to control it our rigid body takes in and that's why our where did it go this Texan this does not get dampened by our this but once the velocity is really high that means we are far enough and you will see we can use this and control the player which just helps us makes it more responsive on the air and helps us with our wall jump as well now finally what we need to do is before but let me just show you all right now when I jump and I can easily control the player as you can see a little bit harder but kinda easy yeah I can control the player way more easily and I can do some more jumps as well but now the main problem is see the wall jump doesn't feel right and the reason why is let's say for me I personally have a habit of just jumping and then moving the player but many of you might have a habit of moving and then jumping that means you're touching the left or right key after that and you try to jump so let's fix that first of all all right otherwise it wouldn't be really responsive as you can see many times yeah even I make mistakes so let's first fix that so to fix that all you need to do is this function next player flip so we will tell if it is not wall sliding so if it is not one sliding then we can flip the player and what's gonna happen is when you are on the wall when you're on the wall you can't move and now I can actually have a really good wall jump as you can see so with that said thank you for watching I hope you liked the video please its master like button if you liked the video don't forget to subscribe please share it if anyone actually want to know how to make a proper wall jump and with that said again thanks for watching and bye [Music] [Music]
Info
Channel: ChronoABI
Views: 12,959
Rating: undefined out of 5
Keywords: Game, dev, Wall jump, Wall slide, Wall jump in unity 2D, Wall slide in Unity 2D, Unity 2D tutorial, Wall jump in unity, Wall slide in unity, Easy wall jumps, Easy wall slide, Wall jump for 2D platformer, Wall slide for 2D platformer, Make 2D Wall Jump, Make 2D Wall Slide, Unity 2D Wall JUmp, Unity 2D wall Slide, unity wall jump, game development, unity 2d, unity tutorial
Id: FXpUb-H54Oc
Channel Id: undefined
Length: 24min 17sec (1457 seconds)
Published: Wed Jun 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.