Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

ViewportPointToRay lands on wrong position

Discussion in 'VR' started by AngeloGebauer, Jun 29, 2017.

  1. AngeloGebauer

    AngeloGebauer

    Joined:
    Sep 29, 2016
    Posts:
    3
    Hello everyone,

    we are using this piece of code:

    Ray ray = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.25f,0.25f, 0));

    To trace a line from the HoloLens. Now one would assume, that the hit of the ray would be somewhere in the bottom left corner of the users viewport. However is isn't. The ray hits in an area of 20-30 cm arround the position it should it.

    Best regards.
     
  2. Unity_Wesley

    Unity_Wesley

    Unity Technologies

    Joined:
    Sep 17, 2015
    Posts:
    558
    Could you provide more information please? It is very hard to understand the issue with the limited information here. It would appear to me that if you are trying to hit the left hand of the view port, I'm not sure what you mean by the 20cm to 30cm around the position? Could you clarify
     
    Last edited: Jun 29, 2017
  3. AngeloGebauer

    AngeloGebauer

    Joined:
    Sep 29, 2016
    Posts:
    3
    OK, imagine the following:
    You want to place something in the users viewport (HoloLens displays). And you want to place it somewhere in front of the user, outgoing from the position of himself and further from x and y on his viewport (HoloLens display).

    I did two quick drawings to show what i mean:



    The first image just shows the user with the HoloLens on. And the ray (red line) being casted.
    The second image shows the users viewport (with a coordinate system) and a red dot ca. at x=0.25 y=0.25. We want to cast a ray from this position. The red dot also represents the position, where the ray we cast, should hit.
    We cast the ray using this code:
    Ray ray = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.25f,0.25f, 0));

    Lets say you then place a cube at the hit location of the ray.
    According to our understanding, the cube should then be visible for the user in the bottom left of his viewport (basically where the red dot is in picture 2).
    This however is not the case.

    I hope this clarifies our problem a little bit.
     
  4. Unity_Wesley

    Unity_Wesley

    Unity Technologies

    Joined:
    Sep 17, 2015
    Posts:
    558
    Sorry we are still a little lost on what exactly you are trying to achieve. It sounds like you would like to change the origin of the ray to x = 0.25, y = 0.25 then cast the ray from that point. If this is the case then you don't want to use Ray ray = GetComponent<Camera>().ViewportPointToRay and it might be better to use a Physics.Raycast. That way you can control the origin and the direction of the ray cast.

    With this line of code : Ray ray = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.25f,0.25f, 0)); The ray origin starts at the camera and the direction will be the x=0.25 y=0.25.

    i.e.
    upload_2017-6-30_10-42-3.png

    Is this what your seeing? Also, do I have the correct understanding of the issue/problem you are experiencing?
     
  5. AngeloGebauer

    AngeloGebauer

    Joined:
    Sep 29, 2016
    Posts:
    3
    Basically this is what we want. I did some screenshots to provide further explenation:

    So basically went want to cast the green one when we have the coordinates x=0.25, y=0.25.
    The red one when we would have x=0.5, y=0.5. And maybe the blue one if we have x=0.75, y=0.5 (or maybe x and y switched in this case. I'm not completely sure about this one).

    Sorry for the late reply.

     
  6. Unity_Wesley

    Unity_Wesley

    Unity Technologies

    Joined:
    Sep 17, 2015
    Posts:
    558
    I would suggest adjusting the x and the y coordinates for the green line. The ray cast appears to be working on my end so far.