Search Unity

How to use Raycast in VR without Mouse Position

Discussion in 'AR/VR (XR) Discussion' started by Reynaldi, May 7, 2020.

  1. Reynaldi

    Reynaldi

    Joined:
    Nov 5, 2016
    Posts:
    14
    Hi!

    I'm trying to make a VR app for android phone, so far everything works out fine until I tried to build the apk and installed it into my phone. When I gazed at everything, nothing works. I did some digging and found out that every tutorial I saw always uses Input.mousePosition while in the phone, no mouse is available. https://github.com/googlevr/gvr-unity-sdk/issues/800

    Code (CSharp):
    1. var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    What can I use to replace mouse position? Or what other method should I try?
    My current code and system is like this:

    VR SDK : GoogleVRForUnity_1.200.1
    Unity : Unity 2019.3.12f1

    Code (CSharp):
    1. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    2. RaycastHit hit;
    3.         if(Physics.Raycast(ray, out hit))
    4.         {
    5.             objectName = hit.collider.gameObject.name;
    6.         }
     
  2. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,933
    The link you provided has the answer already - I'm not sure what more you're looking for? Gaze is nothing to do with the mouse - it's exactly as described on that page: it's your Camera.

    For anything other than gaze, if you don't want to use GoogleVR's solution in the link you pasted, I have no idea the best way around it - GoogleVR is a bit niche.

    If you decide switch to Unity's VR system (XR), I'd open the source code for XRRayInteractor - if you ignore all the member variables at the top of the class, it's a very short class, but it has the complete code for shooting camera rays from the controllers (which is the general-purpose equivalent of shooting a ray from the mouse)
     
    JoeStrout likes this.
  3. Reynaldi

    Reynaldi

    Joined:
    Nov 5, 2016
    Posts:
    14
    I'm not familiar with the function available in Unity, so I'm not sure what to change when I saw the site only says
    transform.worward
    . I've tried to change it to
    Code (CSharp):
    1. Ray ray = Camera.main.ScreenPointToRay(Camera.main.transform.forward);
    But the variable
    objectName
    does not say the name of object I'm looking at. from the image file below, you can see the object I'm looking at is "Level1", but when I play the scene and look at it (this is what I meant by "gaze"), the
    objectName
    says it's "Plane" and not "Level1". Plane is the grassy floor underneath it
     

    Attached Files:

  4. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,933
    You probably need to do more of the Unity core tutorials on the main website first. This is some of the basic stuff, and if you don't feel confident with it, you're likely to get very stuck in some of the VR bits very quickly.

    My guess is that you're using Physics.Raycast ... which - if you read the docs - says that it does NOT return the nearest thing hit, you have to write your own code to decide which was the "first" thing that got hit. But this is coverd in mainstream Unity tutorials (along with lots of other things about Unity scripting you'll need/want to know) hence: you might want to start tehre.
     
  5. Reynaldi

    Reynaldi

    Joined:
    Nov 5, 2016
    Posts:
    14
    Yes, I'm using
    Physics.Raycast
    , and I'm totally confused here. I've searched all over and tried a bunch of stuff with no luck, and I don't know, maybe I just used a lot of wrong keywords to look for what I need. One of the changes I tried which looks promising at first:
    Code (CSharp):
    1.     void Update()
    2.     {
    3.         RaycastHit hit;
    4.  
    5.         if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit))
    6.         {
    7.             Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
    8.             objectName = hit.collider.gameObject.name;
    9.         }
    10.     }
    But it only returns the first thing it sees and does not respond to any other stuff when I move around the camera after running it. I don't know if I'm using a wrong function or did I miss something. Here I attached a picture[1] showing the variable
    objectName
    still not saying the name of the object I'm pointing at and the ray won't direct to any other place after I move the camera.

    And when I was using
    Code (CSharp):
    1.     Ray ray = Camera.main.ScreenPointToRay(transform.forward);
    2.         RaycastHit hit;
    3.  
    4.         if(Physics.Raycast(ray, out hit))
    5.         {
    6.             objectName = hit.collider.gameObject.name;
    7.         }
    It did shows the object name I wanted to look at, but it did when I was looking at the sky. It's as if the point that the camera looking at was below where the reticle was pointing at as shown in picture[2] below.
     

    Attached Files:

  6. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,933
    Did you read the API docs for ScreenPointToRay? If so, why did you give it that input, how did you think that matched what the API docs tell you the parameter has to be? If not -- you *need* to read the API docs for each of this methods. Again: I strongly recommend going through non-VR tutorials first, and learning how some of these core API calls work and what they mean before you try to apply them to a VR project.