Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

Day Three- Whoops, late start!

I really wanted to get the time mechanic sorted right off the bat as this is the core gameplay mechanic and was sure to be fairly challenging...

Getting the time countdown to work was surprisingly easy, in fact! Getting the respective UI bar to display the appropriate amount of time remaining, however, was a bit more fiddly. It took a fair bit of work to discover how even to affect its size during runtime: it turns out that it is not possible to directly manipulate the position in code; rather, it's necessary to manipulate four 'offset' variables, referring to 'minOffset' and 'maxOffset' for the UI element's x and y axes.

Following this I could work out the top and bottom values for 'offsetMax.x' where the bar was either totally full or totally empty, then, working out the percentage of time remaining I'd apply that percentage to the box size and...


...oops.

What had I done wrong? This was actually nearly what I was after and the jittering at the end of the gif is actually the level resetting, as intended, when the time hit 0. But the bar was going the wrong way!

The issue took me a while to figure out but as with all the best technical faults there was a simple solution: What I had been doing was working out the percentage of time left (timeRemaining/maxTime) and applied this to the time bar's offset value. But the offset works in reverse! Rather than the bar starting at 100% size and going down, I was setting it to 100% of the max offset value (i.e. the bar being 100% reduced in size) and decreasing this offset as time progressed. What I needed was the opposite of this; the logical way of doing this being to measure time elapsed, starting at 0% and increasing to 100% of the time limit!

This was a fun little challenge to kick off with as not only do I have limited experience developing with unity's UI tools, but there was also a bit of maths to be worked out to come up with the right solution.

So now, I have a time bar that decreases as time elapses:


And works just as well when adjusted to different time scales - important, because I'll probably need to tweak this a lot later to give an appropriate amount of time for the player to complete puzzles in!