For the past week I’ve been trying to implement a working inventory system. Since I’m new to almost everything in Unity I usually try and find my own way before implementing something that is either to complicated for me to understand yet and/or a bit bloated for my need.
So I’m going with a set 8 slot inventory that is always visible at the bottom of the screen (kind of like Minecraft, but without the bigger inventory).
This is done with an inventory (empty gameobject), that has 8 differents ‘slots’ (also empty game objects, with images). When you pick something up the item gets placed in a “equipped item” and disabled, and the image of an empty inventory slot gets switched for the one of the picked up item.
The ‘equipped item’ is a game object that’s a child of the main camera and when you press the number buttons (1-8), the corresponding disabled child gets enabled.
(Don’t mind the unreadable font)
From here you can choose to press “F” to throw the item. If you do a few things happen:
- The items meshcollider is turned to true.
- A rigidbody is added and it’s collisionDetectionMode is turned to CollisionDetectionMode.ContinuousDynamic (had problems with items falling through the ground before this).
- A force is added to transform.forward.
- The items parent is set to null.
- An Invoke-snippet is used to wait .1 seconds before changing the items tag to “Moving” instead of “Null” as it had while being an equipped item. The items in my game are tagged “Item” when they are on the ground, but I have to remove this when they are equipped, otherwise you can pick them up from your own hand. The reason for the .1 second delay is because of the next action.
- When the items tag is changed to “Moving” an update function in the “Item”-script starts checking to see if the items velocity is equal to not moving. If so it removes the rigidbody and makes the mesh collider not convex anymore. It also sets the tag back to “Item” so that it can be picked up again.
And there you have it, throwable rocks.