Recap: Double Jump
Part of the Building Real Games section of Coddy's Unity journey. Lesson 42 of 56.
Challenge
MediumWrite 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.Moveatspeedunits a second along x and z. - Gravity by hand: a vertical speed that gains
gravityevery second, set to -2 while the hero is grounded and falling. - Space on the ground jumps exactly
jumpHeighthigh. - Space in the air jumps once more, rising
airJumpHeightabove 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
2Buttons and Screens
Buttons and onClickLambdasWiring in the InspectorPanels On and OffHealth BarsRecap: Upgrade Shop5Saving with PlayerPrefs
Save a NumberLoad with a DefaultHigh ScoreSave SettingsRecap: Coins in the Bank8Character Controller
CharacterController.MoveGravity by HandJumpingPushing ThingsMove Where You LookRecap: Double Jump3The Game Manager
One Script Runs the GameThe SingletonGame StatesSwitching on the StateRecap: Round Manager6Project: Endless Runner
Three LanesObstacles Ahead9The New Input System
Two Input SystemsKeyboard and MousePressed This FrameInput ActionsRecap: Input Swap