Equipping armor and weapons is a must when talking about inventory and items. There are two types of equipment, skinned and not skinned. Skinned equipment is any object that gets deformed as the player animates, such as the player’s clothes. Skinned equipment must be imported in Unity with the same rig as the Skinned Mesh Renderer it will skin to. Non-skinned equipment are the other type, such as a sword.
It is particularly hard to create an equipment system that works for all games. The system is customizable and for even more freedom it is possible to replace it with your own. The equipment system is a module which no other system depends on.
Note that the Equipment system does not take care of stat changes. Stat changes are not part of the Inventory System, they are only part of the demo. The demo shows a way of how stats can be implemented for the character. The Character Stats class takes in an Equipper in its constructor which is then used to compute the character stats at any given time. You are free to follow a similar approach.
The equipping system consists of three components: the Item Slot Collection, the Item Slot Set, and the Equipper.
Item Slot Collection
The inventory can have many Item Collections. These collections can be of any type, which means Item Collections can act differently within an Inventory. The Item Slot Collection is a subclass of the Item Collection class which allows us to specify the Item Slot Set. The Item Slot Set restricts what item and how many items can be added to it. In most cases the Item Slot Collection contains the items that are equipped within the inventory so we give it the name “Equipped” and set its purpose to “Equipped”. It is important to set a unique name and purpose within the Item Collections contained in inventory. This is because the Equipper component will access the Item Collection by the name or purpose.
When an item is added to a Item Slot Collection it is first checked to ensure the item can be added to a slot. If it can then that slot is checked to determine if it is empty or occupied. If it is empty then the item can be added without a problem. If the slot is occupied then the existing item is replaced by the newly added item. The removed item will then be placed in the Main Item Collection of the Inventory.
Item Slot Set
The Item Slot Set is a Scriptable Object used to define a set of item slots. A lot of RPGs have item slots for the head, right hand, left hand, chest and leg. But not every project is the same and you may want more or less slots. You can set the slots from the inspector. Item Slots are defined as a Name, an Item Category and a size limit. You can have as many or as little as you need.
As an example consider the Right Hand slot which is defined with a Weapon Item Category and a size limit of 1. In this scenario you are allowed to equip one weapon in your right hand.
The Equipper is used to spawn and visually equip the items within the “Equipped” Item Collection. When specifying the Item Slot set, the Equipper will allow you to specify if an Item Object slot is skinned or not, and if not where the equipped item should be spawned. You can also set the GameObjects that should be hidden when something is equipped in that slot
Two prefab attributes can be spawned by the equipper when equipping an item:
- Equipment Prefab: The Equipment prefab is the prefab that is usually set as an Item Definition Attribute. It will be spawned when the item is equipped. It should have a visual model, either a child Skinned Mesh Renderer or a simple Mesh renderer. Normally this prefab does not have any scripts attached to it, although it can.
- Usable Item Prefab: (Optional) This prefab is usually set as an Item Category Attribute. That’s where you would set the scripts for using your item. For example a script to use a melee attack, or shoot projectiles. This prefab should not have any models just scripts. When used the Equipment prefab will be spawned as a child of this prefab.
The reason we chose to split these in two attributes is to give the option to seperate logic from the model. Allowing you to have a single prefab to define how to do a melee attack or shoot projectile, shared between many items and simply have the model being swapped. Of course an Item Binding component can be used to switch the property values on the Usable Item to change values such as “Attack”, “Number of Projectiles”, etc…
Skinned Mesh Equipment
Skinned Mesh Equipment prefabs must be set such that the skinned mesh can be stripped from the prefab and attached to the character bones when spawned.
The skinned mesh equipment prefab would be set as the “Equipment Prefab” attribute. In most use cases the “Usable Item Prefab” attribute would either not exist or be null.
It is recommended that the top level of the prefab is simply a transform containing two children: the Skinned Mesh and the full character rig.
IMPORTANT: Without the Character rig the item won’t be able to be equipped as it won’t be able to cross reference the used bones with the character bones. Of course the character rig hierarchy on the item must match the character rig it will be equipped to.
Note that only the game object with the skinned mesh will be spawned on the character, the character rig will be discarded to clean up the character hierarchy. For that reason items are pooled per character and not shared between all characters.
As you can see the equipment system is quite simple and it is independent from the Usable Item Objects or character stats. This makes it very easy to extend or completely swap out for something that fits your game requirements.