Loot System: Part Two — Enemy Loot!
Objective: Create a system for enemies to drop diamonds!
In the last tutorial we created our diamond collectables with their own unique behavior. In this tutorial we are going to create logic for our enemies to instantiate diamonds when they play their death animation at their position with an assigned value.
Another benefit to our enemy design is we don’t need to create a game object for every enemy type. Instead we can create a single game object on the enemy class and assign it.
Next we need to instantiate and cast as a game object.
Now we can access the object we instantiated.
Now when we defeat an enemy and they play their death animation they will “drop” a diamond. and the value of that diamond will be set to the gems value we have for that enemy. Our moss giant is set to a gem value of 10.
Now the player’s “gems” value has increased by the amount “dropped” by the Moss Giant.
I’ve noticed a glitch in our current set up that if we attack a dead enemy, they will replay their death animation and drop another diamond.
We can fix this by adding telling the enemy to do nothing(return) if the is dead bool is true.
Now our enemies will drop loot for our player to collect, and they will only drop it once! Don’t be afraid to experiment with your loot drops and I’ll see you in the next tutorial!