CONDITIONALS & JUMP MECHANIC

Your complete step-by-step walkthrough — follow at your own pace, tick every box

Course: Game On (9GAMZA) Lesson: T1 — L08 Time: ~75 min Unity: 2022.3.x Outcomes: G5-1, G5-2, G5-4
TOTAL XP 0 / 50 XP
1
Understanding If Statements
~10 min • Learn conditional logic — the brain of your game.
+10 XP

Key Terms

Conditional
Code that makes a decision: "IF something is true, DO this." The game's brain.
if statement
Checks a condition. If true, the code inside runs. If false, it's skipped.
else
Runs when the if condition is false. "If NOT true, do this instead."
else if
Checks another condition if the first if was false. Chain multiple checks!
Comparison
Operators that compare values: == (equal), != (not equal), > < >= <=
💡
Real-world example: "IF it's raining, THEN take an umbrella, ELSE wear sunglasses." Every game is built on thousands of if-then decisions like this!
1.1
Open your Unity project from last lesson. Your player should move with WASD and have public variables in the Inspector.
1.2
Open your PlayerMovement script. The basic if statement pattern looks like this:
if (condition) { // This code runs if condition is TRUE } else { // This code runs if condition is FALSE }
1.3
Comparison Operators — these go INSIDE the if ( ):
  • == — Equal to (is lives equal to 0?)
  • != — Not equal to (is name NOT "Hero"?)
  • > — Greater than (is score greater than 100?)
  • < — Less than (is health less than 20?)
  • >= — Greater than or equal to
  • <= — Less than or equal to
Common mistake: = is ASSIGNMENT (sets a value). == is COMPARISON (checks if equal). They are different! lives = 0 sets lives to 0. lives == 0 asks "are lives equal to 0?"
1.4
Test it! Add this code at the END of your Start() function (after your Debug.Log lines):
if (lives > 0) { Debug.Log("Player is alive with " + lives + " lives!"); } else { Debug.Log("Game Over! No lives left."); }
1.5
Save, Play, check Console. You should see "Player is alive with 3 lives!". Now Stop, change lives to 0 in the Inspector, and Play again — you'll see "Game Over!".
Checkpoint: "I understand if/else statements and comparison operators. Changing lives in the Inspector changes which message appears."
2
The Jump Mechanic
~15 min • Make your player jump with Space and physics!
+10 XP

Key Terms

GetKeyDown
Returns true on the EXACT frame a key is first pressed. Unlike GetAxis, it's an instant trigger.
Rigidbody2D
The physics component. We use it to apply force (gravity pulls down, jump pushes up).
AddForce
Applies a push to a Rigidbody. We use it to launch the player upward!
ForceMode2D.Impulse
Applies force instantly (like a rocket). Perfect for jumping.
isGrounded
A bool variable that tracks whether the player is on the ground. Prevents infinite jumping!
Important: For this section to work, your Player MUST have a Rigidbody 2D component (from L05). If it doesn't, click Player → Add Component → Rigidbody 2D. Also make sure your ground has a Collider 2D.
2.1
First, let's set up Rigidbody reference. Add this variable at the top of your class (with your other public variables):
private Rigidbody2D rb;
And add this inside Start() (you can keep your Debug.Log lines):
void Start() { rb = GetComponent<Rigidbody2D>(); // your Debug.Log lines can stay here too }
What is this? GetComponent finds the Rigidbody2D attached to this same GameObject. We store it in rb so we can use it later to apply jump force. This is faster than searching for it every frame!
2.2
Now add the jump variable at the top with your other variables:
public float jumpForce = 10.0f;
(You may already have this from L07 — check first!)
2.3
Add the jump code inside Update(), AFTER your movement code:
// Jump when Space is pressed if (Input.GetKeyDown(KeyCode.Space)) { rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse); Debug.Log("JUMP!"); }
Line by line:
Input.GetKeyDown(KeyCode.Space) — true ONLY on the frame Space is first pressed
rb.AddForce() — applies physics force to the Rigidbody
Vector2.up — the direction (up = positive Y)
* jumpForce — how strong the push is
ForceMode2D.Impulse — instant burst of force (not gradual)
2.4
Save (Ctrl+S) and go to Unity. Press Play. Hit Space — your player should JUMP!
2.5
Tune the jump! While playing, try different jumpForce values in the Inspector:
  • 5 — small hop
  • 10 — normal jump
  • 20 — super jump
  • 50 — to the moon!
Remember: Play mode changes are temporary. Stop, then set your preferred value.
2.6
Problem: Infinite jumping! Press Space repeatedly — the player keeps jumping in mid-air forever! This is the "Flappy Bird problem". We'll fix this in Section 3.
What you should see
Player jumps when you press Space. But you can spam Space to fly infinitely (we'll fix this!).
Checkpoint: "My player jumps with Space! But infinite jumping is a problem."
Troubleshooting:
  • Player doesn't jump? Check: does Player have Rigidbody 2D? Is rb assigned in Start()?
  • Player falls through ground? Ground needs a Collider 2D. Player needs a Collider 2D too.
  • "rb is null" error? Make sure the Rigidbody2D component is on the same object as the script.
  • Player rotates when jumping? In Rigidbody 2D, under Constraints, tick "Freeze Rotation Z".
3
Ground Check — Fix Infinite Jump
~20 min • Only allow jumping when on the ground.
+10 XP

Key Terms

isGrounded
A bool that's true when the player is touching the ground, false when in the air.
OnCollisionEnter2D
A Unity function that runs automatically when this object touches another collider.
OnCollisionExit2D
Runs automatically when this object stops touching another collider.
Tag
A label you assign to GameObjects. We'll tag the ground as "Ground" to identify it.
💡
The logic: Set isGrounded = true when the player lands on ground, and isGrounded = false when they leave the ground. Only allow jumping IF isGrounded is true.
3.1
Tag the ground. Click on your ground object in the Hierarchy. In the Inspector at the top, click the Tag dropdown → Add Tag. Click the + button, type Ground, and click Save.
3.2
Now click on your ground object again. Set its Tag to "Ground" from the dropdown. (You created the tag, now assign it.)
Important: If you have multiple ground objects, tag ALL of them as "Ground".
3.3
Add the isGrounded variable at the top of your script:
public bool isGrounded = true;
3.4
Update your jump code in Update() to check isGrounded:
// Jump when Space is pressed AND player is on the ground if (Input.GetKeyDown(KeyCode.Space) && isGrounded) { rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse); isGrounded = false; Debug.Log("JUMP!"); }
What's new? The && means AND — BOTH conditions must be true. Space must be pressed AND isGrounded must be true. When we jump, we immediately set isGrounded to false so they can't jump again mid-air.
3.5
Add collision detection functions BELOW your Update() function (still inside the class). These are special Unity functions that run automatically:
void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("Ground")) { isGrounded = true; Debug.Log("Landed!"); } } void OnCollisionExit2D(Collision2D collision) { if (collision.gameObject.CompareTag("Ground")) { isGrounded = false; } }
How this works:
OnCollisionEnter2D — runs when you land on something. If it's tagged "Ground", set isGrounded = true.
OnCollisionExit2D — runs when you leave something. If it was "Ground", set isGrounded = false.
CompareTag("Ground") — checks if the other object has the "Ground" tag.
3.6
Save and Play! Press Space — player jumps ONCE. Try pressing Space in mid-air — nothing happens! Land on the ground, and you can jump again.
3.7
Watch the isGrounded checkbox in the Inspector while playing. It toggles on/off as you jump and land!
Checkpoint: "My player can only jump when on the ground. No more infinite jumping! isGrounded toggles in the Inspector."
Troubleshooting:
  • Can't jump at all? Check isGrounded in Inspector — is it true? If false, the collision tag might not match. Make sure ground is tagged "Ground" (capital G!).
  • Still jumping infinitely? Make sure you added && isGrounded to the if statement. Also check that OnCollisionEnter2D is OUTSIDE Update().
  • "CompareTag" error? Make sure you created the "Ground" tag first (Inspector → Tag → Add Tag).
  • Player sticks to walls? Create a new Physics Material 2D with Friction = 0, apply it to the player's Collider.
4
Polish Your Platformer — Lab
~20 min • Add lives, game over, and tune your movement.
+10 XP
💡
This section combines everything: variables, if statements, physics, and input. You're building a real platformer!
4.1
Add a "death zone". Create an empty GameObject: right-click in Hierarchy → Create Empty. Name it "DeathZone". Add a Box Collider 2D to it. Tick Is Trigger in the Collider settings. Position it below the screen (Y = -6 or so) and make it wide (Scale X = 20).
4.2
Tag the DeathZone: Inspector → Tag → Add Tag → DeathZone. Then assign the tag to your DeathZone object.
4.3
Add trigger detection to your PlayerMovement script. Add this function BELOW your OnCollisionExit2D:
void OnTriggerEnter2D(Collider2D other) { if (other.CompareTag("DeathZone")) { lives = lives - 1; Debug.Log("Fell! Lives remaining: " + lives); if (lives <= 0) { Debug.Log("GAME OVER!"); isAlive = false; } else { // Respawn at start position transform.position = new Vector3(0, 2, 0); rb.linearVelocity = Vector2.zero; } } }
What this does: When the player touches the DeathZone trigger, lose a life. If lives reach 0, game over. Otherwise, respawn at position (0, 2, 0) with zero velocity (so they don't keep falling).
4.4
Disable movement when dead. At the very START of Update(), add:
void Update() { if (!isAlive) return; // ... rest of your movement code ... }
What is "return"? It exits the function immediately. So if isAlive is false, NONE of the movement code runs. The ! means "not" — so !isAlive means "not alive".
4.5
Save and Play! Walk off the edge — you should fall into the DeathZone, lose a life, and respawn. Do it 3 times — GAME OVER!
4.6
Create more platforms! Right-click → 2D Object → Sprites → Square (or duplicate your ground). Position platforms at different heights. Tag them all as "Ground". Now you have a platformer level!
4.7
Final tune-up: Adjust these variables in the Inspector to get the perfect feel:
  • Speed — try 6-10
  • Jump Force — try 8-15
  • Lives — try 3-5
  • Gravity Scale — on Rigidbody 2D, try 2-3 for snappier falls
4.8
Save everything! Ctrl+S in both Unity and code editor.
What you should see
Player jumps between platforms. Falls into DeathZone = lose a life + respawn. 0 lives = game over, player can't move.
Checkpoint: "My player jumps, falls cause lives to decrease, and game over works when lives = 0."
5
Extension Challenges
If time permits • Level up your platformer.
+10 XP

Only if you've finished Sections 1-4.

5.1
Challenge 1: Better Gravity. Select your Player. In Rigidbody 2D, increase Gravity Scale to 3. This makes the player fall faster after jumping — much more satisfying! Also add this code after the jump to make falling feel heavier:
if (rb.linearVelocity.y < 0) { rb.gravityScale = 5f; } else { rb.gravityScale = 3f; }
5.2
Challenge 2: Variable Jump Height. If the player taps Space quickly, do a small jump. If they hold Space, jump higher! Replace your jump code:
if (Input.GetKeyUp(KeyCode.Space) && rb.linearVelocity.y > 0) { rb.linearVelocity = new Vector2(rb.linearVelocity.x, rb.linearVelocity.y * 0.5f); }
This cuts the upward velocity in half when Space is released — shorter press = shorter jump!
5.3
Challenge 3: Collectibles. Create a new sprite. Tag it "Coin". Make it a Trigger (Is Trigger = true). Add to OnTriggerEnter2D:
if (other.CompareTag("Coin")) { score = score + 1; Debug.Log("Score: " + score); Destroy(other.gameObject); }
5.4
Challenge 4: Coyote Time. Allow the player to jump for a brief moment after walking off a platform (like in real platformer games). This requires a timer variable and coroutine — research "Unity coyote time" for guides!
🎮
Research: Look up "2D platformer jump feel Unity" — game designers have written entire articles about making jumps feel perfect. How do games like Celeste and Hollow Knight make jumping feel so good?