Unity VR: Socket Interaction to Get Things Done

Brian Severa
4 min readMar 27, 2024

--

This article looks at another type of interactor — specifically, the Socket Interactor. The Socket Interactor allows you to take objects and socket them in other objects. Below I’ll look at setting up a Socket Interactor to act as a key hole and when socketed, it will open a door.

The Key Hole

On the Keyhole game object, I’ve attached a Sphere Collider and resized it to be about twice the size of the keyhole. The Sphere Collider will play a role in “snapping the key into the socket”. The collider’s Is Trigger property has been checked, as I want the key to pass through and trigger the socket action to occur.

I’ve also added an XR Socket Interactor component to the Keyhole game object.

Similar to layers, many of the Interactor components have an Interaction Layer Mask property. To keep from being able to socket anything and everything, I created a new layer specifically for the key interaction, called it “Key” and assigned it to the Interaction Layer Mask of the XR Socket Interaction component.

To better position the key, I’ve created an AttachPoint object as a child of the Keyhole game object, positioned it over the Keyhole, and assigned it to the Attach Transform property on the XR Socket Interactor.

Finally, the XR Socket Interactor has a number of events that I can work with:

The Hover Entered event fires when an object interacts with the collider attached to the object. The Hover Exited event fires when an object leaves the collider boundaries of the object the collider is attached to. The Select Entered event fires when an object is socketed and the Select Exited event fires when an object is unsocketed.

I want the door to open when the key is socketed, so I’ll be using the Select Entered event. Before I configure this, let’s look at the door to see what can be used for this event.

The Door

The door has an animator controller on it.

The door has two animation states: Closed and Open. Closed is the default.

A bool parameter called DoorOpen has been defined.

The transition object contains a condition of DoorOpen to move from the Closed state to the Open state.

Keyhole Event Configuration

With a better understanding of how the door’s animator is configured I can now use this information to configure the Select Entered event on the XR Socket Interactor of the Keyhole game object.

I added a new Select Entered event and assigned the door object to the Object property, and set the Animator.SetTrigger (string) function to the Function property. Because the function type is a string, I have to specify the parameter name from the door’s animator, which is “DoorOpen”.

Configure the Select Entered Event

The Key

The final thing I need to configure is the key. The key will obviously need to interact with the Keyhole game object that was configured above.

The Key has a collider on it and it has the Interaction Layer Mask of its XR Grab Interactable component set to “Mixed…” with the Default and Key layers selected.

The Default layer is required to interact with the ray interactor of the hands, allowing me to grab the key. The Key layer is needed to allow the key to be inserted into the key hole by the door.

With everything configured, I now have a key that can open a door!

--

--

Brian Severa

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