Let’s make a Socket Inventory System!
To follow along with this tutorial in Unity, first download the VR template I provide here.
Once downloaded and booted up, you’ll be a be able to download the specific resources needed for this project found here. Just add them to the VR template project and you’ll be ready to follow along!
Working with socket interactors
To make a our Socket Inventory System work, we need to start by working with XR Socket Interactors.
- Expand the XR Origin in the Scene.
- Go to the Socket Inventory Tutorial 2022 folder.
- Drag and drop the BodySocketInventorySystem prefab onto the Camera Offset found underneath the XR Origin.
Now that we have it in the Scene, let’s run through what it’s made of.
The base of the prefab is just an empty game object that we’ve labeled BodySocketInventorySystem.
Also attached is another prefab called RightBodySocket, which has an Attach Point as a child.
Now all of our Sockets are set up for storing Grab Interactable objects that are set to Interactable. Take note that we set the BoxCollilder to Is Trigger. If we don’t do that, then the Socket Interactor will not work.
Last, let’s add some XR Grab Interactables to our scene so we can test out our Socket Interactors.
- Go to the Socket Inventory Tutorial 2022 folder
- Go to the Ultra Low Poly folder
- Drag the UltraLowPolyGun onto the table in the Scene
- Go to the Ultra Low Poly folder
Adjusting Our Attachment
Much like how we fix Grab Interactables using Attach Points, we can do the same for our items attaching to our Socket Interactors.
- The Y Axis should be pointing up in the direction we want our object to point up.
- The X Axis should be pointing towards where we want the palm of our hand.
- The Z Axis we want to have facing in the direction we want our object to face.
In this case, we want the gun barrel to be facing down. To do this, we do the following.
- On RightHipSocket
- Expand and select the AttachPoint
- Drag the Attach Point into the Attach Transform slot found in the XR Socket Interactor component
- On AttachPoint
- Rotation (90, 0, 0)
You’ll notice that the Z axis is pointing down, the Y is pointing forwards and the X axis is point out towards where the palm of our hand would be when we go to holster the gun.
Now that our item is holstering correctly, there is another issue we need to take care of.
When we move up and down or turn by walking around and not using the thumbsticks, our sockets do not follow us correctly. Nothing a quick script can’t fix!
Adding More Sockets
Having one inventory socket is nice, but why not more!?
- Click and drag a BodySocket prefab onto the BodySocketInventory in the Scene
- On the newly created BodySocket
- Position = (-0.288, 0.915, 0)
- On AttachPoint
- Rotation = (90, 0, 0)
Now we can see that we have two squares that will serve as sockets for our left and right sides!
If we wanted to, we could also add a socket in for two chest slots and even a backpack!
For this tutorial just to keep is short and sweet, we’ll leave it at having two Body Sockets so we can figure out how to make these work and track with our head mounted display.
Height and Rotating Our Socket Inventory
In order to have the Socket Interactors follow us, we need to use the tracking of the Head Mounted Display. Luckily, the Main Camera already does that!
- On BodySocketInventory
- On BodySocketInventory Script
- Drag the Main Camera into the HMD (Head Mounted Display) slot
- Expand Body Sockets and click the plus sign
- Drag the RightBodySocket into the GameObject slot
- Adjust the Height Ratio to 0.55
- On BodySocketInventory Script
- Do this again for the LeftBodySocket that we created
We begin with a custom class that requires a Game Object and a float for the Height Ratio. We’ll use this Height Ratio later to determine the height relative to the HMD for different Body Sockets. We can go into greater detail why we need this when we get to the height update function.
The two public variables hold the Game Object for the HMD(Main Camera) and a array of Body Sockets that we will need to go through and update.
We have two private variables that will hold the current position and rotation for the HMD when we need them in the update function.
In the update function, we update the height for each Body Socket in our array. We maintain it’s position for x and z, but we adjust the height based on the HMD’s height and the ratio associated with that body socket. This will help our system be more dynamic if our players variations in player height or if they are crouching.
We wrap things up by updating the position and rotation of the entire SocketInventory. This will keep all our body sockets both in the correct position relative to the HMD and it’s rotation.
Conclusion
So there we have it! The beginnings of a socket inventory system. This setup could easily be extended to add a backpack or ammo pouch in the front with a few more tweeks to the code.
Hope this was useful, and I will see you in the next one!!!