Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Convert Event.current.mousePosition to match Input.mousePosition

Discussion in 'Extensions & OnGUI' started by RobAtApex, Jun 7, 2018.

  1. RobAtApex

    RobAtApex

    Joined:
    Jun 19, 2017
    Posts:
    3
    In OnGUI(), what's the right way to convert Event.current.mousePosition to the same space as Input.mousePosition?

    X is fine, but the Y coords are upside down with respect to each other. So you would think that this would work:

    Screen.height - 1- Event.current.mousePosition.y

    But in the Unity Editor the result is consistently off by 3. Why? Presumably this will be different on different platforms, so hard-coding "+2" instead of "-1" above would be unwise.

    In our app, Screen.height is same as Camera.main.pixelHeight, so using that instead won't make a difference.

    Motivation: to improve accuracy of mouse/touch position at time of click/tap. Currently at low frame rates, we detect a tap during the previous frame but then it's too late to use Input.mousePosition as it's already stale. This results in the user grabbing something to move it, only to find it grabs something else along their swipe-path, not the item they clicked on.
     
  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    396
    I think the formula should be Screen.height - Event.current.mousePosition.y.
    anyways, not sure if the event data is in pixels.

    I think your problem is that you wait for the completed click (mouse down and mouse up). If I read correctly between your lines you actually want to perform your logic already when the mouse is getting pressed / the finger hits the display.
    So, you can either add an EventTrigger component to the objects of interest with PointerDown (and maybe also PointerUp) events - or you have an Update-Method somewhere which constantly checks if the state changes and raycasts the objects of interest itself.