Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Convert Event.current.mousePosition to match Input.mousePosition

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

  1. RobAtApex


    Jun 19, 2017
    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


    Aug 23, 2016
    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.