ScreenPointToRay with Event.mousePosition?

Discussion in 'Scripting' started by SneakySoft, Jan 15, 2012.

  1. SneakySoft

    SneakySoft

    New Member

    Joined:
    Apr 5, 2010
    Messages:
    96
    Hi all,

    I want the world mouse position in my editor window.
    Since I can't use Input in the editorwindow class I use
    the Event.mousePosition in OnGUI.
    Code (csharp):
    1.  
    2. void OnGUI()
    3. {
    4. Event e = Event.current;
    5. ray = cam.camera.ScreenPointToRay(e.mousePosition);
    6. }
    7.  
    Strangest thing, the object I want to move with my mouse position is moving correct
    in the X axis, but is inverted on the Y axis.
    So I inverted the e.mouse position.y like this:
    Code (csharp):
    1. ray = cam.camera.ScreenPointToRay(new Vector 3(e.mousePosition.x,-e.mousePosition.y,0));
    Now the object is moving in the right direction, but the position is totally off...

    How do I solve this issue?
  2. SneakySoft

    SneakySoft

    New Member

    Joined:
    Apr 5, 2010
    Messages:
    96
    if I do
    Code (csharp):
    1. -e.mousePosition.y + Screen.height
    It comes close but it's still of by about 16 pixels. because if I do:

    Code (csharp):
    1. -e.mousePosition.y + Screen.height - 16
    It looks like it's right on spot, but I would have no idea why I would have to do it like this, it sounds a bit strange to me....

    Anyone who know's what is going on here?
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Messages:
    25,589
    It's not strange; ScreenPoint is from bottom-up, like Input.mousePosition, whereas GUI space is top-down. You can just do mousePosition.y = Screen.height - mousePosition.y;.

    --Eric
  4. SneakySoft

    SneakySoft

    New Member

    Joined:
    Apr 5, 2010
    Messages:
    96
    Thanks for responding Eric5h5. Now I understand it :). The only problem remaining is the small around 16 pixels offset. This is only in the y direction.

    Just for testing purposes I have an object that is at the world mouse position.

    Code (csharp):
    1. ray = cam.camera.ScreenPointToRay(new Vector3(e.mousePosition.x, Screen.height - e.mousePosition.y, 0))
    2. mouseWorldPos.transform.position = hit.point;
    What could cause this reaction?
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Messages:
    25,589
    There isn't any offset in the code, so I would assume it's the pivot point of your object.

    --Eric
  6. SneakySoft

    SneakySoft

    New Member

    Joined:
    Apr 5, 2010
    Messages:
    96
    It's not the pivot in this case. I just add a primitive.sphere gameobject at that position, the pivot looks correct, because it's in the center of the gameobject.

    I also know it has to do with the pixels because sometimes the world distance between my mouse and the mouse position object I created is bigger when the camera angle changes. However, the pixel distance stays the same.

    I converted the point from world space back to screen space and I now draw a GUI.Box there. It's pixel perfect with my actual mouse position, while mouse world position is still off in the y axis.....

    I don't feel confident with hard coding -16 pixels. Because then in might be wrong on different pc's. Does anyone know what's going on here?
  7. Jumplegreen

    Jumplegreen

    New Member

    Joined:
    Nov 1, 2012
    Messages:
    2
    I am not sure of the exact reason why but I found this code which solves the problem

    Camera ccam = Camera.current; Ray r = ccam.ScreenPointToRay ( new Vector3(Event.current.mousePosition.x, ccam.pixelHeight - Event.current.mousePosition.y, 0.0f));

    I guess the screen height is not the same as the camera portal height for some some reason.

    This has been working fine for me.
  8. a436t4ataf

    a436t4ataf

    Member

    Joined:
    May 19, 2013
    Messages:
    16
    Yep, never use Screen.height - it's definitely wrong (make yourself a trivial project and you can see this for yourself).

    Screen.height is used in most / all of the source-code examples I could find for Unity, and explains why a lot of plugins have strange positioning bugs every now and then, but when I substituted in Camera.pixelHeight in each case, turned out to be always correct (tried on multiple projects + multiple source examples).

    Thanks, Jumplegreen!
  9. yoyo

    yoyo

    Member

    Joined:
    Apr 16, 2010
    Messages:
    91
    In the OnGUI method of an EditorWindow, Screen.height includes the space for the title tab -- I guess this must be 16 pixels -- even though (0,0) is below the title tab. Instead of Screen.height, use position.height (EditorWindow.position is a rectangle representing the active area of the window.)
    Last edited: Aug 20, 2013