Creating the Jump Pad

First of all, we need to create the Jump Pad. Our Graphical package comes with a very nice jump pad sprite:

First, drag one of them into the map. We need to add a collider in order to prevent the player from going on the Jump Pad. Then, create a child gameobject and name it Jump Pad  Trigger Collider. Next, set as a Tag for this second game object Jump Pad. Add a collider as well, and adjust the Size and Offset in such a way that the collider is just above the Jump Pad. Also, check the isTrigger variable, as shown in the following screenshot:

This is how it should look in the game:

If you remember from the Angel Cakes collecting system, when the player comes in contact with a cake, it is collected. This was done using a special Unity function named OnTriggerEnter2D, which detects when the player entered the collider of the cake. Here, we are going to do something similar.

Open the Movement Component script, and let's add a variable called onJumpPad of type bool:

   private bool onJumpPad = false;

Another variable is needed to determine how much the Jump Pad should push our hero up in the sky. A serialized private float variable will do the trick:

    [SerializeField]
private float jumpPadMultiplier = 2;

Then, we create the OnTriggerEnter2D()  function, in which we check whether the new collider has the tag JumpPad, and if so, we set onJumpPad to true:

   public void OnTriggerEnter2D(Collider2D collision) {
if (collision.gameObject.CompareTag("JumpPad")) {
onJumpPad = true;
}
}

Similarly, we need a OnTriggerExit2D() function to disable the onJumpPad variable by setting it to false:

   public void OnTriggerExit2D(Collider2D collision) {
if (collision.gameObject.CompareTag("JumpPad")) {
onJumpPad = false;
}
}

Finally, in the Jump() function, we need to modify the code. If the onJumpPad variable is true, then the JumpForce is multiplied by the JumpPadMultiplier variable. As a result, if onJumpPad is true, then the force of the jump will be much more:

   public void Jump() {
//Check if the character can jump
Debug.Log(Physics2D.Linecast(transform.position,
groundCheck.position, 1
<< LayerMask.NameToLayer("Ground")).collider);
if (Physics2D.Linecast(transform.position, groundCheck.position, 1
<< LayerMask.NameToLayer("Ground"))) {
if (rb2d.velocity.y <= 0) {
//Perform the jump (multiply by jumpPadMultiplier if
onJumpPad is true)
rb2d.AddForce(new Vector2
(0f, onJumpPad ? jumpForce*jumpPadMultiplier : jumpForce));

}
}
}

Of course, the function will look a bit different if in the previous chapter you used the alternative version. In any case, it's not a problem, since you just need to substitute the highlighted code from the function you wrote. However, the way that we have written the modified AddForce() function above might still be confusing, due to the ternary operator ?. Again, below is shown a simplified version, which does not use the ternary operator. Feel free to choose whatever version makes more sense to you:

  public void Jump() {
//Check if the character can jump
Debug.Log(Physics2D.Linecast(transform.position,
groundCheck.position, 1
<< LayerMask.NameToLayer("Ground")).collider);
if (Physics2D.Linecast(transform.position, groundCheck.position, 1
<< LayerMask.NameToLayer("Ground"))) {
if (rb2d.velocity.y <= 0) {
//Set the finalJumpForce to the current one
float finalJumpForce = jumpForce;

// Multiply the final jump force by jumpPadMultiplier
// if player is on a jump pad.
if (onJumpPad) finalJumpForce *= jumpPadMultiplier;

//Perform the jump
rb2d.AddForce(new Vector2(0f, finalJumpForce));

}
}
}

Save the script, and create a prefab of the Jump Pad you have just created. Place them in your level, and test that everything works as it should. Lastly, pat yourself on the back for your hard work.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset