Search code examples
c#unity-game-enginevirtual-reality

Click On A Game Object In VR and Do Something


Hello I'm developing a Virtual Reality App in Unity and I'm using Steam Virtual Reality, VRTK and HTC VIVE. Like in PC we Use "Void OnMouseDown" to detect clicks on a game object, how we can achieve this in Virtual Reality? Suppose We have Virtual Reality scene with a single Cube. Now what I want is to detect clicks on the objects so how can I achieve this in Virtual Reality? Help Me Please Thanks.

public void OnMouseDown()
{
   //some code    
}

Solution

  • You should refer to this tutorial on VRTK. You can also refer the following example in your VRTK package.

    VRTK/Examples/005_Controller/BasicObjectGrabbing

    You have two parts for this:

    • You will need to add VRTK_InteractTouch & VRTK_InteractGrab scripts on your Left & Right Controllers. And setup grab button to your desired button. By default its set to Grip Press.
    • After you have setup controller its time to setup the object which you want to make interactable. Select your Cube. Go to Windows. Point to VRTK option. Click Setup Interactable Object. A popup will show up called Setup Object. You can go through the options and make desired changes else you can directly click on setup selected object button. It will add the desired scripts on your object. You can also add manually each of these scripts which are shown in the tutorial if you need to else use the options from windows directly to add intractability.

    You also might want to go through this. If you need a grabbed object there is a specific function mentioned in document called GetGrabbedObect.


    As a first time developer it will be bit confusing to use VRTK but trust me its one of the most powerful tool to make VR games. Secondly keep on checking the tutorial videos about using VRTK and go through the examples folder.