[Video][Game Making Tutorial 4] Changing Characters and Double Jumping

Hey everyone, third video in the series. In this video, you’ll learn how to change your player’s character with a button and give the character different abilities depending on which form it is. Also, how to set Jump and Double Jump.

 

The full project for this Tutorial is HERE.

Tools used in this video explained:

  1. Empty GameObject: GameObjects that are empty have no properties by themselves, thus we have to add components to it. We can also use Empty GameObjects as “folders” in which to store different parts of our scene, e.g. we can store the Level’s Layout (all the platforms?), health potions, but in this case we’ll store both player sprites and only allow the Empty GameObject to have the Player’s script, Box Collider

 

  1. GameObjects as Variables: In this case, we’re setting two GameObjects as variables. This allows us to modify their properties through code.

 

  1. SetActive(True or False): this way, we can make anything disappear and reappear, removing all their properties from the scene or making them happen again.

 

  1. Find: This method, used to initialize, will find a GameObject with that same name in the scene. The one detail about this is the GameObject MUST BE ACTIVE at the start of the scene. If you don’t have it activated on Start, Unity won’t find them. It’s recommended that you deactivate them in the Update method or in Start right after you found them.

 

 

  1. Edit -> Project Settings -> Input: Here, we can change all the basic inputs for our game. In this case, I changed Fire3 to “Change” and now I can…
  •  GetButton/Input.GetButtonDown(“ButtonName”): With this, we call the buttons by name. GetButton means “If the player has a button pressed/held”, GetButtonDown means “If the player presses a button NOW”, doesn’t count if the player holds it for a long time.
  1. Update Method aka void Update(): In this method we can set up everything Non Physics related, so we’re going to make the characterselect variable changes here. So we check IF the player is pressing the button Change. Then, if he is, is characterselect 1 or two? Each one makes the other happen.

 

  1. Change the characters! If we have character select 1, we set BatMan to active or when 2, set FishMan to active.

 

  1. Ground Checking: This process is really important but more complicated. We must create a gameobject inside our Player called GroundChecker. Place it at the feet of the simulated player component (around the bottom of the collider is fine). Then, create a Layer called Ground. Apply that layer to the Dock so it’s now Ground.

 

  1. Ground Checking Code:

 

  • Variables: We’ll need a variable Public Transform groundChecker, bool grounded, and Public Layermask groundLayer, the layermask will work to only detect the Ground in our next part.
  • Our bool grounded = Physics2D.OverlapCircle: Physics2D contains a huge set of options to detect things around a point. In this case we’ll literally use a circle around the GroundChecker. The usage of OverlapCircle is:
    1. Physics2D.OverlapCircle(Center of the circle, radius of the circle, layer of the detection): So we’ll do groundCheck.transform.position as the center, the radius can be anything but I tested and think 0.2f is fine, layer will be selected in inspecto, to do this you should put “groundLayer” there.
  • Place everything in the Inspector, now our player detects where the ground is.

 

  1. Jump: We need a Jumpspeed variable. In the Editor, the Jump key by default is Spacebar. So, we check IF our player is grounded and he is pressing the Jump Button down. If that’s happening, we add force to the rigidbody2D in the Y with a new vector2 as showcased in the video.

 

  1. DoubleJump: Now, for doublejump we make a new bool called… that. Doublejump. And when we jump, we set it to true and do an Else If right outside the Jumping setting. In there, if the player is NOT grounded, PRESSES the Jump key and doublejump is true, then we’ll first set his rigidbody2d.velocity in Y to 0 through a new Vector2 (rigidbody2d.velocity.x, 0). We do this to add exactly the amount of force from 0, making it an effective double jump.

 

Any doubts, questions or anything reach me at the Forums or right here in the comments. Thanks for watching and reading!

Share

Leave a Reply

Your email address will not be published. Required fields are marked *