Overview
This recipe shows you how to create custom VR interaction components by extendingMolcaInteractionBase. You’ll implement specialized interaction logic that responds to XR Interaction Toolkit events, integrate with the reference system for step-based validation, and add custom per-frame behavior while users interact with objects. This approach is ideal for creating unique interactions beyond standard grab mechanics—like levers, switches, dials, or custom analog controls.
Prerequisites
- SDK modules: Molca Core, Molca VR SDK installed
- Unity setup: RuntimeManager configured, XR Plugin Management installed, XR Interaction Toolkit package added
- Prior knowledge: MolcaInteractionBase, Base components, RuntimeManager
Step-by-step
Step 1: Create a custom interaction class
Create a new C# script that extendsMolcaInteractionBase and implements the UpdateInteraction() method for per-frame logic.
MolcaInteractionBase automatically binds to the XRBaseInteractable on the same GameObject during Awake, tracks selectEntered/selectExited events, and calls UpdateInteraction() only while isInteracting is true. You focus on the interaction logic without managing XRI event wiring.
Step 2: Add XR Interactable component
Add anXRGrabInteractable (or XRSimpleInteractable) component to the same GameObject as your custom interaction script.
MolcaInteractionBase.Awake() automatically discovers the XRBaseInteractable on the same GameObject via GetComponent<XRBaseInteractable>(). If you need to reference an interactable on a different GameObject, assign it manually to the interactable field in the Inspector.
Step 3: Add ReferenceableComponent for step integration
Add aReferenceableComponent to enable reference-based lookups from VR steps.
MolcaInteractionBase registers itself with ReferenceManager after RuntimeManager.WaitForInitialization() completes. This allows VR steps to find your interaction component by reference ID using ReferenceManager.GetReference<LeverInteraction>(refId).
Step 4: Implement interaction events
Add Unity events or C# events to expose interaction state changes to other systems.System.Action or custom delegates) provide type-safe callbacks for code-based integration. Both patterns work well with the step system for completion validation.
Step 5: Add optional distance-based force release
ConfiguremaxInteractionDistance to automatically release the interaction when the hand moves too far away.
MolcaInteractionBase checks distance every frame in Update() when maxInteractionDistance > 0. If the hand exceeds this distance, it calls ForceRelease() which triggers selectExited on the interactable, ending the interaction gracefully.
Step 6: Test in VR
Enter Play mode with VR active and test your custom interaction.MolcaInteractionBase handles all XRI event wiring automatically. Your UpdateInteraction() method only runs while the user is actively interacting, so you can safely assume currentInteractor is valid and isInteracting is true.
Complete example
Here’s a complete custom button interaction that requires the user to press and hold for a duration:onHoldComplete to a custom step’s Complete() method for step-based validation.
Troubleshooting
- UpdateInteraction() never called: Verify the GameObject has an
XRBaseInteractablecomponent (XRGrabInteractable or XRSimpleInteractable). Check that the interactable is enabled and the XR rig is active. EnsureisInteractingis true by addingDebug.Log(isInteracting)inUpdate(). - Interactable field is null:
MolcaInteractionBase.Awake()only searches the same GameObject. If your interactable is on a child or parent, assign it manually in the Inspector to theinteractablefield. - Reference not found by steps: Ensure
ReferenceableComponentis attached and has a stable ID (check Inspector). VerifyRuntimeManager.WaitForInitialization()completes before steps try to resolve references. Check thatReferenceManagersubsystem is active. - Interaction releases unexpectedly: Check
maxInteractionDistancevalue. If set too low, the hand will exceed the distance and triggerForceRelease(). Set to0to disable distance checks entirely. - Events don’t fire: Verify Unity events are wired in the Inspector (expand the event section and add listeners). For C# events, ensure subscribers are registered in
OnEnableand unregistered inOnDisableto avoid memory leaks. - Hand position jitter: Use smoothing or damping on hand position calculations. Consider using
Mathf.Lerp()orVector3.Lerp()to smooth rotation/position updates. Check XR tracking quality in your headset settings.
Related
- MolcaInteractionBase — base class API and interaction lifecycle
- Base components — shared VR base scripts overview
- Valve interaction — example analog interaction with rotation
- Knob interaction — example analog interaction with twist
- Interactable grab object — complete grab interaction setup with visual feedback
- ReferenceableComponent — reference system for step-based lookups
- RuntimeManager — initialization and subsystem access
- Recipe: Set up a VR scenario — foundational VR scenario setup