Unity 2D Player Move and Jump with Groundcheck Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys more blakey here and welcome back to the brand new tutorial channel for the first tutorial i'm going to be reiterating the player move script fit with a ground check so you can only jump once and a full explanation of everything we do so you can make any 2d object move left and right and jump as well if you enjoy it help start this channel off and subscribe let's get into it as you can see here in our scene we've got our main camera a player and a floor and at the moment if we press play absolutely nothing happens as expected but before we can jump right into the code there are a few things we've got to do to ensure that it will work firstly we need to head to our player and we need to give them a few components and the first one is a rigid body 2d and that's essentially going to mean that gravity is now going to interact with this object so now if we press play the cube is going to drop and of course it's not going to collide with the box because neither of them have another component we're about to add and that component is a box collider 2d so i'm going to type in box we're going to go to 2dx we're in a 2d project and i'm just going to go ahead and add that and now you can see our two components rigid body 2d and box collider 2d is very important that is the 2d version of these objects now if we head over to our scene tab and i'll zoom in just a little bit now if i go to my box collider and hit edit collider which is this little kind of these three dots here you can see we've got these four green lines outside our box and i can drag these little dots and move them and make them bigger and this is going to determine how big the collider is for our box so for example if this was all the way down here it would look like the cube is floating in the air when in reality it's not but i'm going to go ahead and control z that just to get it back to the normal size which it should automatically default to but for whatever reason if you've got a player that is a little bit of a weird shape and you need to adjust it that is how to do that now we need to do the same to our floor except without the rigid body so we're just going to add a box collider 2d again if i go and focus on him with f hit that edit collider you can see we've got this nice subtle green line around it so we know it's in the right place finally the last thing we're going to do we are going to select our player in the hierarchy here and we're going to go to his inspector tab we're going to go to tag right at the top untagged and we're gonna hit this add tag and the reason we're gonna do this is because in the future of your project you're gonna need to define what the player object is and we're also gonna need to do this for the floor so we can decide where the floor is and where the ground is and what we know we can jump on and what we can't so we're going to hit this add sign and we're going to type in player with a capital p is very important and we're also going to make one for the floor as well so we're going to type in f l o r capital f now we need to actually assign them so we're going to go to player tag player and we're gonna go to floor tag floor now that has everything we need to do we need to head into a script and do the rest of the work from there so what we're gonna do we're gonna select our player we're going to scroll down to add component and we're just going to type in player movement just like that and as you can see nothing comes up all that's going to come up is new scripts we're going to hit that then it's going to give us a name by default which is what we just typed in and we're gonna click create and add from there we can select this right click and hit open and it will open your visual studio version of choice so the first thing we need to do is assign some variables in our visual studio code so we're going to go to the top here and we're going to enter it and we need to define a float for speed so we're going to go public so it's accessible in our unity project float and then we're going to call this speed and then remember a semicolon at the end as well we're then going to do the same for jump so it needs to be public again so we can access it in unity float and then hit jump just like that finally we need a private float and this flow is going to be used to assign our left and right movement one more variable we need is we need to access our rigid body so we're going to go public rigid body 2d remember it's a 2d one and we're going to call it rb for simplicity references now like i said firstly we need to assign our move so we're going to type in move and then we're going to use input so we're going to be using the old unity input system this is the old one but it's the one that is more accessible for everybody so it's input dot get axis and then we're going to be using the horizontal string name now we need to assign the velocity of our rigid body so we can actually make him move because at the moment this isn't because at the moment this just is not enough to get him to move so we're going to type in rb so we're referencing the rigid body we made at the top i'm going to do dot velocity so now we're referencing the velocity i'm going to go equals 2 so the velocity of our rigid body is equal to a new vector 2. so then this vector 2 is going to take in two floats our first one is going to be speed multiplied by that move so we're going to be multiplying our move float times whatever we set our speed to so that's float number one which is our x axis float which is the one that matters in this case so then we can set our y float to whatever the velocity on the wires of our player right now so that will not be affecting our left and right movement now if we head back into our unity script if we actually press play here it will not work and this is why we get a error message which is the variable rb of player movement has not been assigned so to fix that we're going to head to our player here and you can see down here we've got a couple of parameters that we need to change so for example our rigid body we are going to drag him in right here and for our speed we can set it to a speed of five now if we hit play and we press a and d you can see our cube actually moves so now the next thing we're going to do is create our little jump so we can't follow the exact same method we did for moving left and right for jumping what we're actually going to do this time is use an if statement so we're going to do if then we're going to use our input system again so input dot get button down so this basically means if a button is being pressed and the button in reference is capital j ump for jump we're going to do rb dot add false and we're going to do new vector 2 and then we're going to do rb.velocity.x and jump we don't actually want to affect the x axis we just want to affect the y so we're going to put our jump flow in here and then a semicolon to close that off back in unity we're going to click our player here we're going to go back down and we're going to assign a number such as 200 for our jump and the reason is such a big number is juice is just due to the nature of how we are making our player jump but using this method it means we have a lot more detailed control on how we want our player to jump and how much buy so now if we want to hit the play button here you can see we can move left we can also move right and if we hit the space bar we can jump another one issue of this currently is we can double jump we can also triple jump and if we really want we can truly fly away and of course that is not what we want so do you remember when we assigned this floor tag to our box right here well now that is going to come in handy so in terms of creating a ground check for our player there are two ways we can go about it one is using a box collider and a method called on collision enter and another one is using a raycast now for simplicity reasons and to make this easy to understand the method we're going to be using today is the box collider one in more complicated projects and more long-term projects i would recommend using the raycast method but i'm going to be making a video for this in the near future so stay tuned and subscribe if you are enjoying this one so the first thing we're going to do we're going to go back up to our variables up here i'm going to add a new one we're going to add a boolean so i'm going to go public ball i'm going to call it is jumping now if you don't know a ball is just a true or false statement so we're going to head down to the bottom here we're going to enter outside of our update function i'm going to do private void on collision enter 2d so you can see it right here it's gotta be the 2d one i'm going to change the collision type to other now you don't have to do this but for me i've just always used this word i find it easier to understand when we're talking about other game objects so we're going to do we're going to do if other so we're referencing this other right here if the collision type then we're going to do got dot then we're going to do dot game object dot compare tag so this basically means if the object we are colliding with has a tag of and remember we assigned it to floor with a capital f and inside our curly brackets we are going to say is jump in is equal to false so what we're saying now is as we collide with the floor that means we're not jumping so we're going to set is jumping to false now we need to kind of do the reverse so we're going to do private void on collision and we're going to do exit this time avoid onclude and exit 2d change our collision type to other and then head inside our curly brackets i'm going to do if other dot game object dot compare tag and we're going to call it floor again now inside our curly brackets we are going to set is jumping to true this time because what we're saying now is if we are leaving the floor so if the game object that we are leaving the collision that means we are jumping now you may be able to spot some loopholes with this for example if we were to just fall off the object then our game is going to think we are jumping but in terms of a simple 2d move and jump ground check this works for now so to finish this off to get it working where we have our if statement right here we need to add another parameter to that so we're going to do if input dot get button down jump and then in between these two brackets i'm gonna hit space and we're gonna use the and function we need to do it twice this represents and and we're gonna do if is jumping is equal to false make sure this has two equal signs here so now what we're saying is if we press the spacebar but we're in the air then this function will not run because it's jumping is true so both of these parameters have to be the case otherwise this will not be able to run so now we can move left we can move right and we can hit jump but if i spam hit jump you can see we only jump once which is exactly what we want so there we have it we have a very simple 2d move and jump that you can apply to any 2d object in your game guys i hope you enjoyed today's very simple tutorial stay tuned for many many more on the channel you may have seen a very similar tutorial on my old channel but i thought i would make a new one on here fully explained so you can just access all of these tutorials in the same place guys i'll thank you all very much for watching and i'll see you all in the next one bye
Info
Channel: MoreBBlakeyyy
Views: 38,063
Rating: undefined out of 5
Keywords: unity, unity 2d tutorial, unity 2d jump, unity tutorial, unity 2d, unity 2d platformer, unity jump 2d, unity tutorial 2d, unity 2d jump c#, unity 2d jump ground check, unity 2d jump tutorial, unity jump, unity3d, 2d, unity2d, unity 2d game, unity ground check 2d, unity platformer 2d, unity 2d player movement, unity jump code, wall jump unity, unity 3d, unity jump animation, unity jump script, unity 2d player movement 2020, how to jump in unity, unity 2d animation
Id: 6xn0Sokihdc
Channel Id: undefined
Length: 11min 13sec (673 seconds)
Published: Fri Nov 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.