Adding Temporary Immunity Post-Damage

You may have noticed that your player’s health plummets quite quickly if they get caught by a couple of Slimes, as they deal damage with each contact of the trigger. This seems horribly unfair to the player as we’re punishing them repeatedly for one mistake. To remedy this, we should add a brief period in which the player cannot be harmed after taking a hit.

To accomplish this, we’re going to introduce time measurement into our PlayerStats script. For a brief period, we’ll prevent the player from being harmed if they contact another damage trigger.

We’re going to assume that, in this game, the immunity is a triggered response to a hit reaction, so no hit reaction means no immunity will be granted. We could ...

Get Learning 2D Game Development with Unity®: A Hands-On Guide to Game Creation 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.