Menu
CoddyTech

Recap: Countdown

Part of the Physics and Gameplay section of Coddy's Unity journey. Lesson 26 of 63.

challenge icon

Challenge

Easy

The StartLight starts the race with a countdown. Its script has the car, seconds (3) and the car's speed (8). Write it with a coroutine:

  • Space starts the countdown: it logs 3 at once, 2 one second later, then 1, and one second after 1 it logs Go! and sets the car's linearVelocity to speed units per second forward (+z).
  • Space during a countdown starts it over from 3. Only one countdown ever runs.
  • A click during a countdown stops it and logs Cancelled.

Press Play and try all three, 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
}

All lessons in Physics and Gameplay