Menu
CoddyTech

Recap: Countdown

Part of the Physics and Gameplay section of Coddy's Unity journey. Lesson 27 of 71.

This recap puts the chapter together: a coroutine that counts down with WaitForSeconds, and its handle kept in a field so it can be stopped.

challenge icon

Challenge

Easy

Start the race with a countdown coroutine.

  1. Write IEnumerator Countdown(): count from seconds down to 1, logging each number and waiting 1 second; then log Go! and set car.linearVelocity = Vector3.forward * speed;.
  2. On Space, stop any running countdown, then start a new one into a Coroutine field.
  3. A click during a countdown stops it and logs Cancelled.

Press Play and press Space: 3, 2, 1, Go!. Then Check.

Try it yourself

using System.Collections;
using UnityEngine;

public class RaceStart : MonoBehaviour
{
    public Rigidbody car;
    public int seconds = 3;
    public float speed = 8f;

    void Update()
    {
        // Space: start (or restart) the countdown. Click: cancel it.
    }

    // The countdown: 3, 2, 1 a second apart, then Go! and the car drives off
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Physics and Gameplay

Practice on your own: Unity playground