Unity VR : Socket Interactors
Welcome back to the next step in our series on Unity VR. When working in VR, it can be difficult to manage items and getting them to fit into place. That’s why we are covering Socket Interactors!
We’ll learn how to make a Socket Interactors, how to use Attach Transforms to position our objects correctly, how to use Layer Masks to only allow certain objects to attach to our sockets and how to use sockets to place items on ourselves!
To follow along, download the Github project found here.
1 : Scene Review
We actually have a ton of things to go over this time! Starting off, we have 3 hats sitting on the table. I was able to find the from the Unity Asset store as a free asset located here. They are already setup to be XR Grab Interactors. I’ve also set up a Locomotion System that uses continuous movement. We also have a wall with 3 hooks on it so we can hang up our hats later. Finally, we have a mirror! This will be used to show off our hats that we can wear on our heads.
2 : Making a Socket Interactor
XR Socket Interactor Attributes
- Interaction Manager – This manager is in charge of all interactions between the interactable object and the interactor. If there is one in the scene, the Socket Interactor will automatically assign itself to it. You can have multiple Interaction Managers if you wanted to customize certain interactions, but I haven’t found a good use case for this yet.
- Interaction Layer Mask – If you want to have only certain objects be interactable, you would use this layer mask to set that up. The default setting is to allow all interactable objects
- Attach Transform – This is the Transform that an interactable object will attach to. If you don’t set one, it’ll use the Socket Interactor’s transform by default.
- Starting Selected Interactable – You can choose objects in the Scene to start off attached to the Socket Interactable using this variable.
- Show Interactable Hover Meshes – If set to true, it will show a hover mesh of how the object will be placed on the socket.
- Hover Mesh Material – The mesh that will be used to show a hovering object.
- Can’t Hover Mesh Material – The mesh to be shown if it is an invalid object.
- Hover Scale – Scale of the hover mesh to be used when displaying a hovering object.
- Socket Active – If set to true, the socket will be active and can be interacted with.
- Recycle Delay Time – Amount of time the socket will refuse hovers after a object is removed from the socket.
One thing that I want to specifically point out is that we set the Collider to be set to “Is Trigger”. If you don’t set the Collider to “Is Trigger”, then the XR Socket Interactor will not work.
Now if we start up the scene, we should see that our object is attaching, but it looks a little funny. We’ll fix that in our next section.
3 : Using Attach Transforms
Since our hat isn’t hanging on the wall correctly, we’ll need to use an attach transform. A quick way to get the orientation right is by playing with the hat in play mode. Do the following to get the right attachment to our hook.
- Press play mode
- Attach the hat to Hook1
- Take off your headset
- While still in play mode, drop down Hook1 to find [Hook1] Attach
- Select [Hook1] Attach and rotate/move it until the hat appears to be hanging correctly.
- Right click on the Transform and select Copy Component.
- Exit play mode
- Right click on Hook1 and add a Empty Object calling it Attach Point
- Select it and right click the transform and choose paste component values.
- Select Hook1 and drag the Attach Point into the Attach Transform found on the XR Socket Interactor component.
We do have one problem though. If we were to add another XR Grab Interactable to the scene like our balls from before, they’d be able to attach to the socket. Let’s fix that now.
4 : Interaction Layer Masks
To prevent just any object from attaching to our sockets, we’re going to need to play with the interaction layer mask. First we need to fix our hats.
- In you Project navigate to Prefabs -> 8 Socket Interactors -> Hats.
- Select one of the hats and locate the XR Grab Interactable component.
- Click the drop down for Interaction Layer Mask and choose add layer.
- Add “Hat” to any of the User Layer lines.
- Navigate back to Prefabs -> 8 Socket Interactors -> Hats.
- Use shift-click to select all hats.
- Locate the XR Grab Interactable component, select Interaction Layer Mask drop down and choose Nothing.
- Click the drop down again and choose Hat.
Now that our hats are only interacting with the Hat Interaction Layer Mask, they will only interact with things that also allow interactions with the Hat Interaction Layer Mask.
Next lets fix the socket to do the same thing.
- Select Hook1 from the Scene
- Click Interaction Layer Mask dropdown and choose Nothing
- Click Interaction Layer Mask dropdown and choose Hat
5 : (Optional) Practice Steps 2 - 4 for Hooks 2 and 3
It’s always good to reinforce learning with practice. I deliberately made two more hooks without any socket interactables on them so we could practice implementing them, adjusting transforms and playing with the interaction layer mask some more.
You don’t have to do this step, but it would be helpful if you want to reinforce the information for yourself.
6 : Let's Wear a Hat!
The entire reason I left a mirror in here is for this sole purpose. We’re now going to put a socket on our heads so we can wear a hat.
- Find the Main Camera located on the XR Origin and select it.
- Right click and create an empty object, call it HatSocket.
- Select the HatSocket object and click Add Component, adding a Sphere Collider.
- Change the Sphere Collider to “Is Trigger”
- Change the Sphere Collider Radius to .1
- Click Add Component, adding a XR Socket Interactor.
- Select the Interaction Layer Mask to Nothing, then set it to Hat so the XR Socket Interactor only interacts with the Hat layer.
7 : Conclusion
That’s it for socket interactors. We’ve learned how to make them, how to attach objects in a specific orientation, how to use layer masks to only attach certain objects and how to use sockets to attach items to ourselves!
I hope you learned something and I’ll see you in the next one!
Excellent work. Thank you