Time for action – creating the countdown logic
Let's set the startTime
variable, and build the logic to handle the counting-down functionality of our clock.
- Set the
startTime
variable:function Awake() { startTime = Time.time + 5.0; }
Note
Five seconds to beat the game is a bit ridiculous. We're just keeping the time tight for testing. You can crank this variable up later.
- Decrease the amount of time on the clock:
function DoCountdown() { timeRemaining = startTime - Time.time; }
- If the clock hits zero, pause the clock and call the
TimeIsUp()
function:timeRemaining = startTime - Time.time; if (timeRemaining < 0) { timeRemaining = 0; clockIsPaused = true; TimeIsUp(); }
- Add some
Debug
statements so that you can see something happening:function DoCountdown() ...
Get Unity 4.x Game Development by Example Beginner's Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.