There are many cases where you’ll want to drop items or currency at runtime. This may be when the enemy dies, a puzzle is solved or a tree was cut down.
There are three ways to drop Items: Item Object Spawner, Drop Item Action and Dropper components
Item Object Spawner
The item Object Spawner is probably the easiest one to use anywhere in code. All you need is to add this component somewhere in you project (it is recommended to add it next to you InventorySystemManager).
Make sure to assign it an ID (default 1), you will use this id in your code to find it. And assign it a prefab for your Item Pickup.
// Get the Item Object Spawner anywhere in your code. var itemObjectSpawner = InventorySystemManager.GetGlobal<ItemObjectSpawner>(itemObjectSpawnerID); // Spawn an Item using an item Info and a position. itemObjectSpawner.Spawn(itemInfo, spawnPosition); // There are options to spawn with a delay, an auto destroy or even both. itemObjectSpawner.SpawnWithDelay(itemInfo, spawnPosition, delay); itemObjectSpawner.SpawnWithAutoDestroy(itemInfo, spawnPosition, autoDestroyTimer); itemObjectSpawner.Spawn(itemInfo, spawnPosition, delay, autoDestroyTimer);
Drop Item Action & Quantity Drop Item Action
The drop Item Actions can easily be added to an item Action Set to be used in the UI. There are options for the drop offset and radius, etc…
The most designer friendly way to do spawn pickups is to use Droppers. This not only works for Items but also for inventories and currencies.
Simply add one of the dropper component on a gameobject (like an enemy). The dropper components are:
- Item Dropper
- Currency Dropper
- Random Item Dropper
- Random Currency Dropper
The Item and Random Item Droppers work with both Item and Inventory Pickups.
The Random Item Dropper is a good component to spawn pickups when an enemy dies. This component allows you to specify an inventory where the amounts for each item will be used to create a probability table. The higher the amount, the higher the probability it will get dropped. A drop radius can then be set, along with the item pickup prefab.
The Random Currency Dropper works in a similar manner except the random amount of currency is decided by a normal probability with a minimum and maximum range. This range is specified by a delta percentage offset from a base currency amount of a Currency Owner.
Once your droppers are set in the inspector, to drop the objects you can get a reference to the component and call the “Drop()” method. If you wish to drop a specific item you can call “DropItemPickup (item)” or “DropInventoryPickup(itemList)” or change the contents of the Inventory attached to the drop component.