Unity Community


Results 1 to 9 of 9

  1. Posts
    96

    ScreenPointToRay with Event.mousePosition?

    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:  
    1. void OnGUI()
    2. {
    3. Event e = Event.current;
    4. ray = cam.camera.ScreenPointToRay(e.mousePosition);
    5. }

    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:  
    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. Posts
    96
    if I do
    Code:  
    1. -e.mousePosition.y + Screen.height

    It comes close but it's still of by about 16 pixels. because if I do:

    Code:  
    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. Volunteer Moderator
    Posts
    23,697
    Quote Originally Posted by SneakySoft View Post
    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.
    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
    SpriteTile: new tile system that works seamlessly with Unity 4.3 sprites
    FlyingText3D: dynamic 3D text with TTF fonts | Vectrosity: fast & easy line drawing
    Nifty utilities! Stitch terrains together - runtime model importing - file browser - fractal landscapes


  4. Posts
    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:  
    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. Volunteer Moderator
    Posts
    23,697
    There isn't any offset in the code, so I would assume it's the pivot point of your object.

    --Eric
    SpriteTile: new tile system that works seamlessly with Unity 4.3 sprites
    FlyingText3D: dynamic 3D text with TTF fonts | Vectrosity: fast & easy line drawing
    Nifty utilities! Stitch terrains together - runtime model importing - file browser - fractal landscapes


  6. Posts
    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. Posts
    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. Posts
    4
    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. Location
    Vancouver
    Posts
    75
    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 by yoyo; 08-20-2013 at 02:03 PM. Reason: clarity

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •