In this lecture, we're going to finish off our timer script.
We're going to be calculating the fill amount for our timer image, which will connect up in the next
lecture.
And we're also going to add a couple of additional hooks that our quiz script can use to talk to our
timer.
So let's dive in.
So next, let's have a look at the fill fraction for our image.
So over on our quiz canvas.
If we go down to our time image, our fill amount is ranging anywhere between one and zero.
So we want to say, based on the amount of time that we've given our timer, we want to normalize that
between zero and one and return a fraction.
So let's jump back to our script for a little bit of math.
And I'm actually going to change our if statements here just a little bit.
I'm actually going to change them around.
So instead what I'm going to do is say if the timer value is greater than zero, then we want to figure
out this fill fraction amount for our time image.
Else we want to change the timer over and set all our values.
So a little bit of a change there, but nothing too drastic.
It's just reframing our if statement and this is just a personal preference thing of mine, so feel
free to leave yours as we've had it if you prefer it that way.
So on to figuring out this fill fraction amount for our image.
At the top of the script, let's create a new variable to store that value.
This value is again going to be needed by our quiz script so it can adjust the actual image sprite on
our canvas.
So let's make a public float.
And again, you may want to make this private and add getter methods and things like that.
But again, it's a very small game and I'm not too worried about lots of things trying to change this.
And I'm going to call this the fill fraction.
With our variable set.
Let's head back down to update timer and figure out what's going to go in this if statement.
Well, with a little bit of very simple math, we can just say that the fill fraction is going to be
equal to the current time of value divided by.
In this case, the time to complete question to explain this math.
So I'm not just giving you code.
Let's imagine that we have a time to complete question of 10 seconds.
And a time of value of 5 seconds.
This gives us five divided by ten, which would give us 0.5, so a half would be halfway through our
time.
If we instead change our timer value to say zero.
So it's just run out.
This would give us zero divided by ten.
Which would give us zero.
And at the other extreme of this, if we had a time of value of ten, so it's just started, we'd have
ten divided by ten would give us one.
So our fill fraction has been nicely constrained between zero and one, which is what our UI element
expects, and everything should work nicely.
So with our math done, let's just copy and paste this line and drop it into our other if statement
below, so in our else block.
But this time we're going to say that the fill fraction is equal to our time of value, divided by our
time to show correct answer.
And to test this out, let's go down to our debug log message here and let's add a bit more detail to
this so we can see exactly what's going on.
So let's add the is answering question toggle.
But then going to have our current time of value for that state.
And then finally we're going to add in our Phil Fraction.
So this is just for testing, so it doesn't matter how pretty it looks, but hopefully this format should
look okay in unity.
So let's save up our script and jump back into Unity to test this out.
If we clear our console, let's go ahead and hit play.
And if we pause very quickly, we can see here that we are currently in the answering state.
The answering state, if you remember, has 5 seconds on the clock.
So here with 3 seconds left on the clock, we're around 0.6 of the way through.
And if we carry on playing just a little bit more and let that timer run down, we'll see that as the
timer runs down, the fill fraction gets closer and closer to zero.
So here with 0.4 seconds left on the clock, we have a fill fraction of around 0.08.
Let's carry on playing to see that tick over here we can see the answer state is now false.
So we're in the showing correct answer phase of our game.
We've got around 9 seconds on the clock and a fill fraction of around 0.9, so almost completely full.
So our timer is working nicely.
It's quickly changing states when we need it to, keeping track of the remaining time in that state
and also giving us a nice fractional value that we can pass over to our quiz script to update our timer
image.
There are two more things that we want to do in this time of script, so let's jump back.
One thing I'm going to want in here is another public variable, which will talk to our quiz script
and let it know when it needs to show the next question.
So let's create another public bool and let's call this load.
Next question.
We want to set this to true in our timer script.
So let's have a think about where this should live.
And if we run through the logic, we won't say that if we are not answering a question.
So we're going to be in our else.
BLOCK And the timer has also run out.
So essentially where we're setting this is answered question to true and resetting our timer value,
we're also going to set load next question to true in here as well.
Now you might be wondering why we're not just reusing this is answering question and that's because
we want to be able to set this load.
Next question to false from our quiz script.
And it doesn't really have anything to do with the logic of the timer itself.
It's purely a hook into our timer script that will become a lot clearer when we actually hook it up
in the next lecture.
So for now, just put a pin in that for the moment and we will come back to it.
But the last thing for this script is going to be a way of cancelling our timer early, because once
we've answered a question in our quiz, we don't want to have to sit and wait for this full 30 seconds
to run out.
We want to jump straight to the solution.
So let's create another method in here.
And this one is going to be public this time so that our quiz script again can talk to our timer.
It's not going to need to return anything.
So let's make it void and we'll call it cancel timer.
This is super simple.
So I have a quick think and see if you can figure out what this is going to do.
And I'm not going to give you too long or this is going to do is set our timer value equal to zero.
So in our update timer, this will basically just hotwire this timer value minus equals delta time.
It will throw timer values straight to zero so that when we hit our if statement, it's going to change
its state for us.
So that is our timer script all set up quite a lot of work in this one little script, in this one video,
but in the next lecture we're going to hook it all up to our quiz script and actually see our timer
in action on the canvas.
So I'll see you there.