Day 122 — Creating a Loot System in Unity

Hey and welcome!

The plan here for this one is that when we kill an enemy we’re going to make a Gem spawn which will vary in value depending on the enemy. The player will then keep a hold of these gems in order to purchase items from a shop which we’ll be implementing at a later date.

Let’s start off by creating the Diamond we’re going to use, slice up the sprite sheet for the Diamond or for a custom collectable you have and then add the first sprite into the hierarchy. Add a Box Collider 2D and Rigidbody 2D, set the layer to the same as your player and then create the animation for it using the animation window so that it creates an Animator Controller as well.

In your Player script add in the following variable:

public int diamonds;

You can name it anything you want so long as it’s synonymous with the collectible you’ll be picking up. Now create a new Diamond script to attach to your Diamond object in the hierarchy and add in this code:

public int gems = 1;private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
Player player = other.GetComponent<Player>();
if (player != null)
{
player.diamonds += gems;
Destroy(this.gameObject);
}
}
}

This will add a value to the diamonds variable in the Player script depending on the value that we assign to gems in the inspector, currently the default is set to 1.

Prefab your Diamond object and then add in this bit of code to your Enemy script:

[SerializeField]
protected GameObject _diamondPrefab;

As you can guess you’ll need to assign your Diamond prefab to this through the inspector for each of your enemies. We’re going to need a reference to this prefab so that we can Instantiate it.

When you’ve done that it’s now time to edit the Damage() method across your enemy types once more with this bit of code:

public void Damage()
{
if (isDead == true)
{
return;
}
Health--; if (Health < 1)
{
isDead = true;
anim.SetTrigger("Death");
GameObject diamond = Instantiate(_diamondPrefab, transform.position, Quaternion.identity);
diamond.GetComponent<Diamond>().gems = base.gems;
}
}

The if statement for checking if the enemy is dead is there so that the death animation doesn’t play over and over when the player takes a swipe at them. It also makes it so that only one Diamond will spawn when the enemy dies.

When the enemy health is less than 1 this is where we Instantiate the Diamond prefab where we also assign it to a local GameObject variable called diamond so that we can access the Diamond script and change the gems value to the gems value of our enemy.

So if an enemy’s gems value has been assigned the value of 3 in the inspector then the player will have 3 added to their diamonds variable.

We’ll look into creating the shop system next time so that we can spend these hard earned gems!

--

--

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
Connor Fullarton

Hey and welcome! My name is Connor and my goal here is to put out a daily post for a full year about my game development journey.