top of page
  • Writer's pictureVOiD1 Gaming

Box Bot - Unity Tutorial 2

[UNITY TUTORIAL] [BEGINNER] [DOUBLE JUMP TUTORIAL]

In the previous tutorial, we got to learn how to import assets, design the entire level, add colliders, physics component and finally how to code and make our player move smoothly. But as you might have experienced, our player jumps continuously up even it’s not in the ground.

In this tutorial, we will learn how to detect the player standing on the ground and also how to make a double jump in the air. Further, we will also get to know how to make objects interactive like pushing the crates etc. At the end of this tutorial, you will be able to polish your game well and set up the entire level.


So let’s start improving our game -


1. Arrange all the similar game objects like Ground and Crates that we have duplicated and let’s place them under a Parent Game object.


2. Now select the Player and double click on the Movement script to open in the Visual Studio.

We have to first detect whether our player is touching the ground or not. We can check it by using Physics2D and Layermask. Physics2D helps in casting a circle that detects collision on the desired LayerMask with the specified radius which in turn results in a boolean form. We can always check for the boolean and get our player do certain operations.


3. Let’s add a reference to LayerMask, a float to check the radius, a transform to generate the circle and a boolean to store the result of the Physics2D.


4. Now we have to check for the player standing on the ground. We can write isgrounded=Physics2D.OverlapCircle(transform,radius,layermask) Physics2D.OverlapCircle creates an imaginary circle around the transform position with the given radius and checks for any overlapping with the given LayerMask. It returns true if the condition satisfies which we have stored in the isgrounded variable. We can check the isgrounded boolean only if jump is true.

We have written this method in FixedUpdate function because we are dealing with Physics and as discussed earlier we need to implement all the physics related operations in the FixedUpdate to make sure our game works well and there is no frame drop Like Update Function.


5. Save and get back to Unity. Select any ground object child and create a Layer by clicking on the add layer in the inspector. Again assign that layer to all the child Ground Game object. Do this same method for crate as well if you want our player to jump over the crate. Now head your way to Movement component attached in the Player and select ground layer, crate layer from the drop-down menu. Also increase the radius to .05. we need a point where we can create a circle to overlap and check for collision.


6. Right click on the Player Game Object and create an Empty Game Object. Now position the Empty Game Object to the feet of our Player. This is the position where we want to check for our Player grounded. Play and you can see our Player only jumps when he is in ground and standing over crates. It’s time to make our Player jump twice on air.


7. Head back to Visual Studio and let’s first create a variable that stores the number of jump we want our Player to have. Create an Integer with a value of 2 for a double jump.


8. Now we will make our Player jump twice if he is on air, so to make that, we have to check if the button is pressed then we will jump and also we will make sure to decrease the value of the number of jumps. Again we will check if the Player is on the ground and the button is pressed with the number of jumps already completed and is 0, we will just make our Player jump without tweaking the value of the number of jumps. Also, we will change the number of jumps back to 2 if we are grounded. This will help us to reset the value and every time we are grounded we have the chance to jump twice on the air.


9. Save your script and get back to Unity. Increase the value of the number of jumps to 2, you can, however, change this value as per your need. You can Play and test, the player has now the ability to jump twice on the air.


10. Lastly, let’s make our objects dynamic rather than being static every time. Select a crate and add a Rigidbody2D. As we have discussed earlier, we use Rigidbody2D to implement all the physics on the specified element we have attached the component with. So after adding the Rigidbody2D increase the mass to 10 which will make the object feel heavier. And yes, you can now push the object easily.


Congratulations on completing your second step towards the vast ocean of Game Development. We got to learn how to detect Ground using Physics2D and Layermask, we also learn how to make our player jump twice in the air. Lastly, we made the objects in the environment dynamic so that our player can interact with it.

Graphics Assets - Box Bot Graphics Assets

bottom of page