2D Mobile Game: Loot Drop System


In this article, we cover how to assign different values for loot that is dropped by different enemies when they are destroyed.

Gem Script

After importing and slicing the Gem sprite sheet, making it a game object and the create animation for it, we will want to attach a Rigidbody and Collider so it can detect collisions.

We are going to make the gem a prefab so we can instantiate or create it when the enemy is killed.

After creating a script for the gem and attaching it to the prefab, we want to OnTriggerEnter to see what has collided with the gem. We will check the collider of the other game object by looking at the tag and if the tag is that of the “Player, we will destroy the gem game object to make it appear as if it was collected.

The Player is going to need to keep count of the amount of gems they have collected.

In the Player script, we create an int variable for the gem count.

We will want to add to this gem count when the Player collides with the gem, so we will create a public method (AddGems) with an int parameter. This amount will be added to the Player’s gem count when called.

Back into the Gem script we are going to create a public int variable (gemValue) that will determine the value of the gem collected by the player depending on the enemy that was killed. For testing prupose we will give it an initial value of 2.

In the OnTriggerEnter, we will get a handle on the Player component through the other Collider, call the AddGems method and pass in gemValue that will be added to the Player’s gem count before destroying the gem game object.

We see it working in the inspector, when the player collects the gem the count increases to 2. We can delete the giving the gemValue and initial value in the Start because that will be determined by the enemy that was killed.

Instantiating Gem

In the Enemy Class script, we are going to create a private or protected GameObject variable (gemPrefab) and give it a serialized attribute so we can assign the prefab to the each of the enemy scripts in the inspector.

In each of the individual enemy scripts, we will Instantiate the gemPrefab that was assigned in the inspector, give it the position of the enemy, and keep the rotation the same using Quaternion.identity.

Right now, we have the gem being instantiated and we can collected. We want to add to the Player’s gem count, the amount that is determined by the type of enemy that was destroyed.

Assigning Gem Value

The Enemy Class has a gem int variable that allows the inheriting scripts to determine the value of the gem that they drop when they are destroyed.

We will need access to the gem script since it has the method for adding to the Player’s gem count. The instantiate prefab will be stored in a variable (gem) as a game object. Once we have the handle on the game object, we get the Gem script component and assign the gem value to be added to the Player’s amount to the base value set in each of the individual enemy scripts.

Bug Fix

The loot drop system is working except for the enemy can be killed over and over again. Each time it is killed, it drops more gems so the Player has access to unlimited gems.

In each of the enemy scripts we have a Damage method that has a bool (isDead) that is equal to true when the health is less than 1. We will use this bool to return nothing from the method when it is true, so the enemy will no longer play any of the death logic over and over agin.




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store