2D Player Movement and Jumping in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] all right what's up everyone so in this video we're gonna have a quick look at how you can get a simple move and jump script for a 2-d player and I just have a 2d scene here and all I have is this ground spray and this player spray and that's it for now so the first thing I'm gonna do is I'm just gonna drag in our ground sprite here and I'll drag it down a bit and I just want to add a box Collider 2d onto it so if we come into here and I'm just gonna click on this to adjust it a bit so I'll just put it below the grass and that's all we need for our grounds here and actually while we're at it we're gonna create a tag and if you don't already have it just add a tag here and create a new one and call it whatever you would like and but I just had mine spelled grounds and then just apply that tag right there so that's all we need to do for the ground sprite now I'm gonna drag in our player and she's way too big so I'm gonna adjust it maybe fourth of the size and that looks pretty good and now out of box Collider 2d to him as well and out is adjusted with this and I'm going quick here obviously you could be a lot more particular about this but something like that and the reason I don't want to put on the very bottom is I'll show you in a second but it's gonna help us with the jump script so I'm just gonna leave it like that and then secondly we just need to add a rigidbody 2d and we can just leave all the default settings the way they are so now I'm going to come down into our assets window and we're gonna right-click and create a new excuse me a C sharp script and I'm just gonna call it something like move to D and then I'm going to click on its open it up so then inside of here we're going to constantly want to check if we're moving our players so we're going to say vector 3 and we're going to call it movement and that's going to be equal to a new vector3 and for the exposition we want to say input.getaxis and we're looking for the horizontal axis since it's the taxes and then I want to say 0 on the y and 0 on the Z and then we can say transform dot position plus equals movement times time Delta time times move speed and we don't have this move speed variable yep so I'm going to come up here and say public float move speed and that's going to be equal to 5 so what this is doing essentially is it's this creating a vector3 variable called movement and it's setting it equal to a new vector3 and the x-value is going to be our horizontal input so basically our left and right keys or a and w keys or a and D keys I'm sorry and then 0 on the Y and 0 and the Z and then it's just constantly adding to our game objects position so our players position it's adding that movement in Sector 3 and it's multiplying time dots out the time to get a constant rate in case our frame rate would go up or down and then it's just multiplying again by just move speed here and that's just so you can adjust it to go faster slower and I made a public so you can do that easily in the inspector so if we would save this and go back inside of unity here and we just give it a second to think we can actually add this move T this move to D script to our player so I'm going to click on them and say move to D and it says it doesn't exist for some reason ok I just had a problem with saving the file quick but now I have it saved and I just added the script right here to our player and if we go ahead and run it you can see now he'll fall to the ground and we can go back and forth button right and that's pretty good so that's all we want and like I said you can adjust the speed here so if we move it to 10 he'll go twice as fast and yeah you can play around with that however you'd like ok so now we're just going to look at having some kind of a jump teacher so we needed some kind of check on the bottom here to see if our players actually touching the ground because if not we're gonna build it is constantly jump and I'll show you what I mean here so I'm going to open up this script again know how to close it because I had that error ok so now that we're inside of here I think we're gonna call a new method down here and I'll say void jump just make it easier to understand her to follow and inside of fear I'm just going to say game object getcomponent and I'm looking for the rigidbody2d and then I just want to add a force and I'm gonna say new vector2 and for the x value I would want to give it any x value and then maybe something like five for the Y and then I'm gonna say force mode 2d not impulse and now up here I'm going to say if input that get button down and the button we're looking for here is the jump button so if we're pressing the jump button then we're going to take this code and put it inside of this here so we're pressing the jump button then we're going to take the game object so in our case our player and it's going to get the rigidbody 2d component on the player and it's just going to add a force to that with a vector two of 0 on the X and 5 on the Y and then I'm just gonna use his force mode to do impulse down here and that's just the type of force that it's being added so then you can come up inside of the update method here and say jump and I will go back inside of unity so now when we run the project or player will fall to the ground we can still move like before and now we can actually jump here and you see it works fine but the only problem is if we're in the air we can still continue to jump so that's obviously a problem you can just continuously jump forever so the easiest way at least I think the easiest way to do this is we're just going to add a child to our player so we're going to say we're going to add a empty object here and just call it something like ground check and I'm gonna go into right here and just drag it to about right there and then I want to add a box Collider 2d to this ground check and if you remember before we didn't drag the player's box Collider exactly to the bottom here because that's what this Collider is going to be for so I'm just gonna put it right about there and make it really skinny something like that and expand it to about there and that looks pretty good so we can collapse this so what I'm gonna do now is we're going to come down here and I'm going to create a new script and I'm going to call this one grounded and I'm gonna open this up as well so inside of here we're just going to need two methods so on collision answer 2d and also on collision exit 2d and both of them are basically going to be the same methods it's going to be if our collision dot Collider dot the tag is equal to ground and we're gonna just copy this and paste it inside of here so if you remember before in the very beginning of the video I gave that tag of ground to our ground object so whatever you gave your tag there just make sure you spell it exactly the same here and that'll make sure that every time you hit that ground object we're able to do something so I'm gonna go inside of our move 2d scripts here and I'm gonna create a public pool is grounded and I'm gonna set that equal to false and I'm gonna go back inside of our grounded script here so now we need a way to actually access our player so I'm going to say game object player and at the very start since our our ground check as a child of our player all we need to do is say player is equal to game object dot transform dot parent that game object and that'll just get us our player so now we have a player here right at the start then we can save player dot get component and move to D is grounded equals true so if we're if we collide with the ground then we're going to set is ground it to true and then we exit the ground we're going to say cleared out getcomponent that is grounded equal to false so we can save that and that's all we need for this scripts here and then inside of fear right here where we're checking if we can jump or else we're going to say and is grounded equals true and save that and now we can go into unity and that's really all we need here so give it a minute to think and it looks like we have an error so I'll just see what the promise here oops had to there so we'll go back in here and now all we need to do is just click on our ground check here and click on or add that script called grounded and then side up here you can see we have this public pool that's says is grounded and we can run it and it should be true as soon as we hit that ground so there you go now we can jump and now we can't jump and now we can jump again and again but now we're in the area we can continuously jump so there you go guys I hope you enjoyed this video if you did please give a like and subscribe if you're new and I'll catch you next time [Music]
Info
Channel: Kyle Suchar
Views: 282,504
Rating: 4.8524399 out of 5
Keywords: unity jumping, unity2d, unity2d jumping, unity2d movement, unity movement, game developement, how to jump unity, coding, code, unity 3d, unity3d
Id: L6Q6VHueWnU
Channel Id: undefined
Length: 10min 17sec (617 seconds)
Published: Fri Nov 30 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.