Search Unity

Mouse position on multiple displays

Discussion in 'Input System' started by qudek77, Dec 1, 2020.

  1. qudek77

    qudek77

    Joined:
    Jan 5, 2019
    Posts:
    4
    Hey guys,

    I'm really having trouble with finding out on which display my mouse cursor is/click.

    Is there a way to get the mouse position when the mouse is on display 2,3 etc... or is there an option to find out on which monitor the mouse is so I can check it with the Display.displays class.

    All my functions and logic works only on the first screen, and since I'm using Input.mousePosition for finding out where the mouse is, and the displays in the game view are stacked, maybe there is a confusion on which display I pressed the mouse...

    If anyone knows how to do it properly, please help.

    Thanks in advance.
     
  2. yixinl

    yixinl

    Joined:
    Jun 2, 2021
    Posts:
    1
  3. ul_antnasce

    ul_antnasce

    Joined:
    Dec 13, 2022
    Posts:
    3
    Yep this worked for me, but only works in Play mode, not in the Editor it seems.

    Code (CSharp):
    1. Vector3 r = Display.RelativeMouseAt(new Vector3(mousePos.x, mousePos.y));
    2. int displayIndex = (int)r.z;
    3. Debug.Log("Display Index is: " + displayIndex);
     
    qudek77 likes this.
  4. farazk86

    farazk86

    Joined:
    May 31, 2017
    Posts:
    195
    Hi,

    I found this thread via a Google search after trying to make my mouse input work with a second display.

    I already have a code that works on a single display where a mouse click is detected in worldspace and an object is instantiated there.

    Code (CSharp):
    1. if (Input.GetMouseButtonDown(0))
    2.         {
    3.             Vector3 screenPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    4.             //tracking mouse position.
    5.             Vector3 currentScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z);
    6.  
    7.             Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenSpace);
    8.             // move the modifier to this position
    9.             flowModifier.transform.position = new Vector3(currentPosition.x, flowModifierStartingPos.y, currentPosition.z);
    10.            
    11.         }
    I tried changing the above code to take the second screen into account, as the canvas is rendered on the first screen and the game view is rendered on the second screen

    Code (CSharp):
    1. if (Input.GetMouseButtonDown(0))
    2.         {
    3.             Vector3 screenPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    4.             //tracking mouse position.
    5.             Vector3 currentScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPosition.z);
    6.  
    7.             Vector3 currentPosition = Display.RelativeMouseAt(currentScreenSpace);
    8.             // move the modifier to this position
    9.             flowModifier.transform.position = new Vector3(currentPosition.x, flowModifierStartingPos.y, currentPosition.z);
    10.  
    11.         }
    But no click is being detected and its not working on the second screen..

    Can anyone please suggest how to take into account clicks being detected on the second screen?

    Thank you
     
  5. dananqqdeqd

    dananqqdeqd

    Joined:
    Nov 17, 2022
    Posts:
    3
    Display.RelativeMouseAt method just supports Windows and macOS .
    I have same question on Android.
     
  6. farazk86

    farazk86

    Joined:
    May 31, 2017
    Posts:
    195

    For anyone finding this post and is still struggling with this, I managed to find a solution. Its as simple as wrapping any call to
    Input.mousePosition
    with
    Display.RelativeMouseAt
    .

    So the code to detect mouse clicks on multiple displays changes to:

    Code (CSharp):
    1. if (Input.GetMouseButtonDown(0))
    2.         {
    3.            
    4.             Vector3 currentPosition = Camera.main.ScreenToWorldPoint(Display.RelativeMouseAt(Input.mousePosition));
    5.             // move the modifier to this position
    6.             flowModifier.transform.position = new Vector3(currentPosition.x, flowModifierStartingPos.y, currentPosition.z);
    7.            
    8.  
    9.         }
     
    ckriger and h_beaton like this.
  7. h_beaton

    h_beaton

    Joined:
    Jan 29, 2023
    Posts:
    1
    My friend... this is the single most helpful thing I have ever seen in a unity forum. I have scoured countless forums about this issue and was not able to find a single solution that worked as intended. You are an absolute genius, thank you so much for your help kind stranger
     
    farazk86 likes this.
  8. The_Pied_Shadow

    The_Pied_Shadow

    Joined:
    Jul 25, 2018
    Posts:
    14
    The documentation for Display.RelativeMouseAt says "returns Vector3.zero when Multiple Displays are not supported". I'm getting a constant Vector3.zero return from it so I guess multiple displays are not supported? But the documentation doesn't give any indication as to why this might be.
     
  9. The_Pied_Shadow

    The_Pied_Shadow

    Joined:
    Jul 25, 2018
    Posts:
    14
    I realized this is only the case when being used in Editor. The documentation should really mention that...better yet this is a huge limitation in testing a multiscreen UI and should be fixed.