Time for action – giving it a weakness

The easiest way to weaken our enemies is to give them some health that is reduced when they are shot, and to destroy them when they run out of health:

  1. We start by creating a new script and naming it Health.
  2. This script is rather short and starts with a single variable. This variable will keep track of the remaining health of the tank. By setting the default value to 3, the tank will be able to survive three hits before being destroyed.
    public int health = 3;
  3. This script also contains only one function, Hit. As in the case of the targets, this function is called by the BroadcastMessage function when the player shoots at it. The first line of the function reduces health by one point. The next line checks to see ...

Get Unity Android 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.