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
EasyStart the race with a countdown coroutine.
- Write
IEnumerator Countdown(): count fromsecondsdown to 1, logging each number and waiting 1 second; then logGo!and setcar.linearVelocity = Vector3.forward * speed;. - On Space, stop any running countdown, then start a new one into a
Coroutinefield. - 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
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Physics and Gameplay
1Rigidbody
Gravity and MassPush with AddForceJump with an ImpulseFixedUpdate for PhysicsVelocityKinematic BodiesRecap: Launch Pad4Coroutines
InvokeInvokeRepeating, CancelInvokeYour First CoroutineLoops in CoroutinesStopping CoroutinesRecap: Countdown10Project: Arena Escape
The ArenaHunters2Collisions
OnCollisionEnterWhat Did I Hit?Break on ImpactStanding on the GroundBounce and FrictionCollider ShapesRecap: Breakable WallPractice on your own: Unity playground