Creating a loot system | Unity
A quick guide about how to create a simple loot system in Unity
Objective: Create a loot system for the player to collect diamonds in a 2D game with Unity.
In the last post I covered how to implement a hitbox detection system with Unity. Now, it’s time to create a loot system in order for our player to be able to collect diamonds in our 2D game.
To start, let’s take a look at the current state of our 2D game. Currently, we can attack and receive attacks from the enemies:
Also, we already have several diamonds across the environment that we’ll be able to collect with the loot system:
Creating the diamonds
In order to introduce the respective diamonds we sliced a sprite sheet that contains the diamond sprite with an animation using the 2D sprite editor:
Then, we created a new gameobject with the next components:
- Sprite Renderer
- Rigidbody 2D
The gravity scale is set to 0 so that the diamond doesn’t fall and remains on its position.
- Circle Collider 2D
The Is Trigger property is enabled to check when the player collides with the diamond and handle what to do when that happens in the code.
Once we have those components we can turn the gameobject into a prefab and put them across the 2D environment as we wish:
Creating the loot system
Now, in order to create our loot system, let’s start by creating a new C# script that we’ll attach to the diamond prefab:
Once created, let’s open the script and:
- Include the System namespace at the top.
- Tell the script to require a Rigidbody 2D and a Collider 2D to avoid having to check if these components are included in the gameobject.
Then, let’s create a new public static Action delegate that will serve as a way to notice each time that a diamond gets collected:
If you want to know more about Action delegates you can visit the Microsoft docs:
Action Delegate (System)
Encapsulates a method that has no parameters and does not return a value. public delegate void Action(); public…
Then, let’s use the OnTriggerEnter2D method to check when another collider collides with the diamonds. Once that happens let’s:
- Check if the tag from the other collider belongs to the player (as we want the player to collect the diamonds).
- Check if the Action delegate has someone subscribed to the event to execute it.
- Destroy the diamond gameobject.
Once we attached the Diamond script to the prefab, let’s open the Player script and create a new variable that will keep the count of the diamonds collected:
Finally, let’s use the Start or Awake method to subscribe the player to the Action delegate from the Diamond script. This way we can use an anonymous method that will add a single diamond to the variable we created:
For more reference about the anonymous methods you can visit the Microsoft docs:
Lambda expressions - C# reference
You use a lambda expression to create an anonymous function. Use the lambda declaration operator => to separate the…
And now, if we run the game in the Unity editor, we’ll see that the diamonds get collected when the player collides with them:
And that’s it, we created a loot system that allows the player to collect diamonds in our 2D game with Unity! :D. I’ll see you in the next post, where I’ll be showing a tutorial about UI events in Unity.