Tip of the Day: Loot System 101 in Unity

Mohamed Hijazi
Level Up Coding
Published in
3 min readJun 3, 2021

--

Looting is at the core of most games these days. From looting weapons to health packs to even looting coins or any game currency.

In this article, I wanted to shed light on this system to start understanding the basics of this system. Let’s dive into a practical example.

Loot System 101

In our mobile project (follow progression here Mobile Project Progression Report) the currency is going to be Diamonds.
For our loot system, Diamonds will be scattered around the world, but also we want them to drop from enemies where each enemy will give us a different value for the Diamonds. So one enemy can give a Diamond with a value of 1 and another with a value of 3 for example.

A. Creating the Diamond

The Diamond is going to have a trigger collider and a kinematic Rigidbody to allow the player to pick it up, and we will add a “Diamond” script to it.

A very important method in the above script is the SetDiamondValue(int value) method. We will call this method from the enemy script to determine the value of the dropped diamond.

As for the triggered animation “bounce”, this will allow the Diamond to bounce when it is dropped by the enemy, but most importantly, the collider has been disabled during the duration of the bounce. This is useful so the player won’t just collect the diamond the second he kills the enemy. Now the player knows that there is a diamond to pick up.

As you can see, the collider has been disabled for the duration of the animation.

Finally, do not forget to prefab the diamond.

B. Enemy Script

In the enemy script, we want to know three things.

  1. The diamond value for each enemy and the diamond prefab
  2. The method that will be used to Instantiate the diamond
  3. When will the above method be called

Diamond Value: Simple we will create a variable that we can set in the inspector so each enemy will have a different diamond value. Also we will create a variable that hold the Diamond prefab.

Diamond Instantiation Method: This method will be responsible for instantiating the diamond prefab and then calling the SetDiamondValue method from the diamond script.

When to Instantiate: So when the moment the enemy dies, we will call the InstantiateDiamond Method.

Now, each enemy that dies will drop a diamond that has a specific value to it. This method can be applied to items, for example when breaking an item you drop a diamond.

Here it is in action

This is just scratching the surface of a loot system. From here you can switch the diamond for weapons or items with different rarities and values. We might discuss this in an upcoming article.

--

--

A knowledge seeking biologist who is following his passion into a full time career in Unity / Game Development. https://www.linkedin.com/in/mohamed-hijazi/