Search Unity

Question Click on Terrain with XR toolkit?

Discussion in 'XR Interaction Toolkit and Input' started by mcbauer, Apr 20, 2022.

  1. mcbauer

    mcbauer

    Joined:
    Oct 10, 2015
    Posts:
    524
    Hello devs, how would I go about clicking on a terrain with the XR Interaction toolkit? I would like to be able to click on the terrain, and basically interact with it the same way I do with a mouse, or on mobile. For example, click on the terrain to instantiate a model where I clicked.

    I have an app that I have made that I need to port to the Oculus, if I can figure out what I need to do, I'd be in a great position.

    Thanks in advance!
     
  2. mcbauer

    mcbauer

    Joined:
    Oct 10, 2015
    Posts:
    524
    Bump? I cant believe no one has wanted to click on a terrain with the Oculus....
     
  3. trnq1ll0

    trnq1ll0

    Joined:
    Aug 30, 2019
    Posts:
    50
    Hi, I'am looking for some kinde of the same.
    I don't click on a terrain, but I want to click on something "non-interactive" in my world. And the best I could come up with is to put a simple interactable on it, put another script in the corresponding event and process it there further. But I hope this is not Unity's preferred solution because it feels so wrong.
     
  4. VRDave_Unity

    VRDave_Unity

    Unity Technologies

    Joined:
    Nov 19, 2021
    Posts:
    275
    The XRRayInteractor has the TryGetHitInfo() method that will hand back a position, along with the normal and whether the target is valid (an interactable). Alternatively, you could use TryGetCurrentRaycast() which will hand back a RaycastHit (Physics Collider) and RaycastResult (UI) which you can use to determine what object was hit and what you want to do with it.

    You could add a component to grab data from the the XRRayInteractor or extend the class to a 'XRSpawner', which would wait for some input event (like a button press) and then call the TryGetHitInfo() or TryGetCurrentRaycast() to get the resulting position in the world. You will also need a collider on the mesh (terrain or otherwise) to make this work, but it should not require using a SimpleInteractable to work unless you want to register the object with the interaction manager and filter it that way.
     
    mcbauer likes this.