Unity VR: Hat Trick! Using a Socket Interactor to Wear a Hat

Brian Severa
3 min readMar 30, 2024

--

I’ve looked at how to create an interactive key hole in a previous article. There are other applications for socket interactors. You can use them to decorate yourself! This article looks at using an XR Socket Interactor to wear a hat.

I’ve already set up my scene. I have a table in front of a mirror and a construction hat that I downloaded from Filebase and imported into my scene.

Hat on a table with Mirror in background

Modifying the XR Origin Rig

Since I’m going to be wearing a hat, I’m going to have to modify the rig (the object that represents me in the scene).

The main camera represents my eyes in the scene. What I want to do is add a sphere as a child to the Main Camera, position where we want the hat to sit and remove the Mesh Renderer to make it invisible. I also need to check the Is Trigger property on the collider to allow the hat to pass through it, rather than collide with it.

I’ve added the XR Socket Interactor component to the sphere and added a new Interaction Layer Mask called “Hat”. I only want objects in the “Hat” Interaction Layer to interact with this socket. This will prevent me from putting a sword or a gun or some other object on my head.

Modifying the Hat

To get the hat to interact with the Socket Interactor, I need to add an XR Grab Interactable component to it. I then just need to adjust the Interaction Layer Mask to include the Hat layer.

Finally, I need to wrap the object in colliders that loosely fits the shape of the hat.

With that, I should now be able to wear the hat and see myself in the mirror!

There are a ton of applications for the Socket Interactor. You could use them to holster a gun in a shooter game. You can use one to decorate yourself with different clothing, or use it to help facilitate an inventory system, just to name a few.

You might be wondering how the mirror in the scene works… I’ll take a look at that implementation in a future article.

--

--

Brian Severa

I am a senior software engineer/architect and an aspiring game developer, currently enrolled in a learning program with GameDevHQ.