Recipe 3 Unity6 - RosoVRgarden/VRgarden-tutorials GitHub Wiki

#VR recipe 3: Grab Interactable (Sonic Cylinder)

The sonic cylinder is an object that has physical properties (rigibody) and that plays sound if you select it with your VR hands. Find the sonic cylinder in the hierarchy:

  1. Create a 3D game object (a cylinder) then add <XR Grab Interactable> component to get started. It should automatically install a <Rigidbody>. You can choose to have gravity (to fall) or be kinematic (to stay in the air). Make sure that there is a collider attached to your object (there should be one installed automatically). Test it in VR and your object should have an object with physical properties.

    You need three things: Collider (skin) + Rigidbody (physics gravity) + XR Grab Interactable

  1. The sound. Add a <Audio Source> component then import an AudioClip in your assets (I recommend sounddogs.com for reasonably priced professional sounds). Drag the sound from the assets to the AudioClip (example: parasite). Make sure that Play on Awake is off.

  1. Go back to the <XR Grab Interactable> component and look for the Interactable Events. You can choose when will the trigger start an event. I have chosen on select entered which when you press trigger on the controller. Click + then drag the sonic cylinder from the hierarchy to where the sonic cylinder is. Then click next to Runtime Only and select AudioSource > Play(). When you stop selecting (stop pressing the trigger), your event is the On Select Exited. Click next to Runtime Only and select AudioSource > Stop() or Pause() if you want to be able to resume where you stopped.

  1. Additionaly, if you want to change the scale of the object while being interacted, you can do so using the XR General Grab Transformer.

  1. Exercise: Add other events such as a Mesh renderer to change the colour of the material as you enter and exit the Select event.

  2. More: Check the XR Grab Interactable page on the Unity Manual: https://docs.unity3d.com/Packages/[email protected]/manual/xr-grab-interactable.html

⚠️ **GitHub.com Fallback** ⚠️