Search Unity

[solved] ScreenPointToRay ( Input.mousePosition ) offset error

Discussion in 'Scripting' started by Antypodish, Aug 31, 2018.

  1. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,776
    Either I am tired and I am missing something tremendously obvious, or there is some form of bug in Unity 2018.2.

    My ray from cam.ScreenToViewportPoint ( Input.mousePosition ) don't intersects the point, where expecting to. I got significant offset error. Red cube is only 20 units apart from the camera. and error gets bigger, further you go.

    I used this method many times in past. Had no major issues. But now?
    See the screenshot, and basics script.
    There are no other cameras, or scrips active.
    Any thoughts?

    upload_2018-8-31_21-48-30.png

    Code (CSharp):
    1. Camera cam = Camera.main ;
    2.         Debug.Log ( "Mouse Pos " + Input.mousePosition ) ;
    3.         Debug.Log ( "Mouse Pos : Screen To Vieport " + cam.ScreenToViewportPoint ( Input.mousePosition ) ) ;
    4.         Ray ray = cam.ScreenPointToRay ( Input.mousePosition ) ;
    5.         Debug.Log ( "Ray " + ray ) ;
    6.         Debug.DrawLine ( ray.origin, ray.direction * 100, Color.red ) ;
     
    unity_voqFO6OzmWES5w likes this.
  2. xred13

    xred13

    Joined:
    Jul 14, 2018
    Posts:
    74
    It seems like the same problem that I had, I fixed it by casting a plane and getting the point on that plane, when I get home I can post the code for you to see if it fixes it
     
  3. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,776
    Thx,
    Did you experienced this also in Unity 2018.x?

    Edit
    Just checked 2018.1.0b13. It has same issue.
    Eh can not get back to 2017, as I need ECS.
     
    Last edited: Aug 31, 2018
  4. xred13

    xred13

    Joined:
    Jul 14, 2018
    Posts:
    74
    here's a function i have that gets me vector3 of the mouse on the world when its called, try it out


    Code (CSharp):
    1.     Vector3 getCursorWorldPosition()
    2.     {
    3.         //finding point in world in cursor position
    4.         Plane groundPlane = new Plane(Vector3.up, new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z));
    5.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    6.         float distance;
    7.         //simply initializing vector3 point, nothing else, this vector zero does nothing
    8.         Vector3 point = Vector3.zero;
    9.         if (groundPlane.Raycast(ray, out distance))
    10.         {
    11.             point = ray.GetPoint(distance);
    12.         }
    13.  
    14.         return point;
    15.     }
     
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,776
    Thx again.

    Howeverm this unfortunately is not exactly what I am looking for.
    Thing is, I need my ray, to find the objects. In your case, you assume you know plane object position already, before searching for the point intersection on the plane.
    Which isn't the case for me. If my ray don't hit the object, then I will never find it.

    Which leads to the next point
    Code (CSharp):
    1. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    Of which is the source of problem. The output ray in my understanding from ScreenPointToRay is incorrect.
    Which puts me back in the square one position.
     
  6. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    Code (CSharp):
    1. Debug.DrawLine ( ray.origin, ray.origin + ray.direction * 100, Color.red );
    2. Debug.DrawRay ( ray.origin, ray.direction * 100, Color.red );
    :)
     
  7. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,776
    Oh seriously? I feel so dumb now so much lol :D
    Such obvious, how I missed this out, even used draw ray so many times. ;)
    Stupidly (I don't know how) I assumed ray from 0 origin lol.

    Thx lot.
     
  8. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    I know that feeling and now always think twice before debug-drawing a ray. :)
     
  9. Crycks

    Crycks

    Joined:
    Aug 3, 2018
    Posts:
    3
    I just got the same problem... And i found this post. Just in case it happens to someone else, my problem was comming from the FOV of the camera (there was a difference between my camera and the Cinemachine)
     
    VrischDev and singapurchik666 like this.