Menu
CoddyTech

Recap: Double Jump

Part of the Building Real Games section of Coddy's Unity journey. Lesson 42 of 56.

challenge icon

Challenge

Medium

Write the platformer hero's movement in Hero.cs; the Hero has a CharacterController. The Inspector sets speed (5), gravity (-25), jumpHeight (2) and airJumpHeight (1.28).

  • The arrows (or WASD), read with the raw axes, walk the hero with controller.Move at speed units a second along x and z.
  • Gravity by hand: a vertical speed that gains gravity every second, set to -2 while the hero is grounded and falling.
  • Space on the ground jumps exactly jumpHeight high.
  • Space in the air jumps once more, rising airJumpHeight above the point where it was pressed. One air jump per time in the air: landing gives it back.

Press Play, double jump onto the ledge, then Check.

Try it yourself

using UnityEngine;

public class Hero : MonoBehaviour
{
    public float speed = 5f;
    public float gravity = -25f;
    public float jumpHeight = 2f;
    public float airJumpHeight = 1.28f;

    CharacterController controller;

    void Start()
    {
        controller = GetComponent<CharacterController>();
    }

    void Update()
    {
    }
}

All lessons in Building Real Games