Zombie Swipe: Zombies Dropping Health Packs on Death

As I work more on Zombie Swipe I am trying to stay focused and implement the core elements of my initial vision first and foremost. But there are a couple things that have cropped up that interest me and I thought this one was pretty easy to implement the other night during a coding session. After squashing a few bugs I have it working pretty much how I want it to be, but will need to work on customizing it to be more useful.

What I wanted is for health packs to occasionally spawn every X number of Zombies slain by the Player. I knew I would need pieces of code that would:

  • Replace the Zombie sprite with a health pack sprite
  • Give the Player X amount of health when pack is clicked
  • Only spawn the health pack at a certain percentage rate

I used these two Unity Answers to help get an idea of what needed to be done:

How to spawn prefabs with percent random?

Instantiating droppable items on destroy game object

With those concepts combined I created a HealthPack class that would heal the player OnMouseDown() and stubbed in a red cross for the dropped health pack.

The results:


I am fairly pleased with how clean the code is, compared to some other parts of my code.

The SpawnHealthPack() method is called from the Update() method of the Zombie.cs class every time a Zombie dies.

public void SpawnHealthPack()
        while (zombieKillCount == zombieTarget)
            zombieTarget += targetIncrement; //Resets the zombieTarget amount so player
                                             //has to kill targetIncrement more  
                                             //zombies before next spawn chance

            Vector3 position = transform.position; //Get the Zombie object's current position

            if (Random.value <= 1) //Percent rate Health Pack spawns at. 
                GameObject healthpack = Instantiate(hpPack, position, Quaternion.identity);
                healthpack.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);


And the HealthPack class, thus far, is barebones and does its job.

public class HealthPack : MonoBehaviour

    Player player;

    public GameObject healthpack;

    public int hpUp = 5;

    void Start()
        player = GameObject.Find("Player").GetComponent<Player>();


    void OnMouseDown()

At some point I’d like to add animations to the health pack and destroyed Zombies. Until then, this will do.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s