Can you do a game without a jump? Of course you can. But I think you should know how to make your character Jump. Jump is not a simple movement up and down right? It is a push off the ground, which has force and acceleration. And then the object goes down because of gravity now because we told him to do so.
Therefore, we work with physics here. Let's take a look at this Collider. We have created colliders for our sport, man and the ground, but we have a problem. When we are in a plane mode, you'll see that I was worried man doesn't stay on the ground. There, colliders cannot cross each other. So I decide to delete this collider and Create box one.
Okay, let's open our Sparkman go to capsule collider and remove component. All right. Now let's add another component box collider 2d. And it's Collider. It should be just the way our character is so that his legs can stay on the ground. Alright, maybe too big.
We have another collider for our sport man. And right now if we are on if we turn on that plane mode, we see that our player and take On the ground, but we still have this little little line. Because here's our ground Collider. Here's our character's collider and they cannot cross each other. How he can push off to jump. I'll tell you, let's go in our prefab and create a second collider like this, but it should be but it should have a part which is under our characters Collider.
And the next step is to select the second collider with that we have done and fine Is trigger and check it. Trigger colliders are used as triggers so they can and will cross other colliders. Alright, let's check it in the plane mode. So you see that our characters collider does not cross the grounds Collider, that the second collider is located inside the ground Collider. So yes, it does cross it. The next step is to add a tag to the ground so that we can define it by its tag.
Let's select our ground, find tag here and add tag And now we can simply select the tag ground from the list. All right time to code I use the same script goes for me jump is part of movement. First of all, let's create a variable which are going to help us with ground check. I remind you that serialized field makes a private variable visible from the inspector but it remains private. Okay, let's create a private variable type. Bull bull type means it can be true or false.
The name is is grounded and it is false. Next, we will use Another function that Unity has it is void and it is called on trigger. Enter 2d. This function is called when the collider others enters that trigger. We have a trigger on the second collider on our character remember alright so inside this function we check if there we check if other collider has a tag ground, by chance and if so is grounded true oh and we need to edit it we need to type other here all right. So again we take the other collider and check if this collider is tagged by ground and then the object is grounded and now vice versa.
We add a function, the private void the function on trigger exit to the again either here and I can simply copy it up and this time false on trigger exit to the is called When the collider other has stopped touching the trigger, all right, we have our checking. Let's go ahead inside the update function we already have our right left movement and let's add something or jump. I will use the curly brackets because you know it's a good a good practice to always use curly brackets because it doesn't understand that we have more than one line inside it. We cannot do it if we have only one line after that, but it is best practice. All right, so get component You can use this symbols if you'd like to add several conditions. So, in this case it means if this and this at once, first part here means if a user press that space button, that jump means space button.
Again you can check all this things inside the properties here. Again edit Project Settings input. So if a user presses space button and that variable is grounded, which we have declared, is true. And this second condition saves us from endless jumps. Our character jumps on If he is grounded the next part, and of course, where this is a direction of acceleration, and this is that instant impulse. You can change the number here and change acceleration that way.
Let's save it. Go back to unity and check it. Okay, but spacebar, yay, jumping. And if I want to if I press that pace while the character is in the air, nothing is happening because now Anthony jumps on the one jump from the ground, but of course, maybe you need endless Maybe so right now our sport man moves and jam, which is great, but he looks too boring without animation and it is waiting for you in the next lessons.