Recap: Countdown
Part of the Physics and Gameplay section of Coddy's Unity journey. Lesson 26 of 63.
Challenge
EasyThe 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
3at once,2one second later, then1, and one second after1it logsGo!and sets the car'slinearVelocitytospeedunits 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
1Rigidbody
Gravity and MassPush with AddForceJump with an ImpulseFixedUpdate for PhysicsVelocityKinematic BodiesRecap: Launch Pad4Coroutines
InvokeInvokeRepeating, CancelInvokeYour First CoroutineLoops in CoroutinesStopping CoroutinesRecap: Countdown2Collisions
OnCollisionEnterWhat Did I Hit?Break on ImpactStanding on the GroundBounce and FrictionCollider ShapesRecap: Breakable Wall