Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. 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:
    2
    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:
    350
    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.