Item pickups are components used to “pickup” items using an Interactable (the Item Pickup) and an Interactor (the character). There are multiple types of pickups:
- Item Pickup
- Inventory Pickup
- Currency Pickup
- Random Inventory Pickup
The Pickup Base class inherits the Interactable Behavior class. This class is used to add interactions to a GameObject and can be interacted with by any Interactor.
Note that the Item Pickup component is not the Interactable, it is an Interactable Behavior. For the Item Pickup component to work an Interactable component needs to be added. It works for both 2D and 3D trigger events.
There are three actions that can be performed on a Interactable component: select, deselect and interact. Select and deselect can be used to show an indicator when an Interactable is in range. With Item Pickups we use select and deselect to display the name of the item when the character is close enough to interact with it (pick it up).
The Item Pickup component uses the Item Object component to decide which item is picked up. In some cases you may want the Item Pickup to display differently depending on the item attached to the Item Object. In the demo scene the Item Object Visualizer component listens to the item being set to the Item Object. This indicates when the mesh/sprite of the Item Pickup needs to be updated.
With this approach you can have a single Item Pickup prefab that works for all your items and you can customize how it looks from the inventory system editor.
The other types of pickups work in similar ways:
Currency Pickup works using a Currency Collection. When picked up it will give the Interactor the amount specified on the Currency Collection.
The Inventory Pickup allows the Interactor to pick up multiple items. All the items in the inventory will be copied, and the copies will be given to the Interactor.
Random Inventory Pickup
The Random Inventory pickup is similar to the inventory pickup with the difference that instead of picking up the totality of the Inventory, the component will use the Item Amounts as a probability table. This can be used for loot boxes. You can set up a range for how many items will be picked up and the component will give the Interactor any random item within that change using the Inventory as a probability table.
Item Pickup Requirements
Character GameObject Requirements:
- Rigidbody: A Rigidbody/Rigidbody2D component and at least one Collider/Collider2D component.
- Inventory: An Inventory component.
- Inventory Interactor: A component inheriting the “Interactor With Inventory” interface. It allows you to interact with objects, like pickups, and let them know that you have an Inventory, allowing pickup items to be added to that inventory. Without an “Interactor With Inventory”, the character won’t be able to pickup items.
- Note the layer layer in which the character is set as it is used by the interactable component.
Item Pickup GameObject:
- Item Pickup: An Item Pickup component (or any of the other types of pickups).
- Interactable: An Interactable component, Rigidbody/Rigidbody2D, and at least one Collider/Collider2D component.
- Trigger Collider: The collider must be set as trigger.
- On the Interactable component the 2D or 3D trigger events should be selected, as well as the layer of the character.
- Item Visualizer: The Item Object Visualizer or Inventory Item Visualizer can be used to change how the item pickup looks like at runtime.
There are multiple ways Items can be dropped. The most common ones are:
- The Item Action: Drop Item Action
- The Item Action: Drop Quantity Item Action
- The Item View Drop Action (aka drag&drop action): Spawn Item Object (this uses an Item Object Spawner in the scene with a matching ID)
- The Item Object Spawner component (usually on the “Game” gameobject)
- Spawning with a custom script
Dropping an item pickup with a custom script simply requires spawning the Pickup prefab and then setting the Item on the the ItemObject component.