Search Unity

What Display Did I Touch/click?

Discussion in 'Scripting' started by ben4d85, Apr 13, 2019.

  1. ben4d85

    ben4d85

    Joined:
    Dec 26, 2018
    Posts:
    47
    Using Unity 2018.3.11f1, I am building an application that spans across two separate screens. How can I determine which screen a touch (or mouse click) occurred on?

    Below please see my attempt. The problem is that for this to work, I need a way to determine which camera/display to use for my raycast hit test. How do I do that?

    Code (CSharp):
    1. // if the left/primary mouse button has been pressed
    2. if( Input.GetMouseButton(0) || Input.GetMouseButtonDown(0) || Input.GetMouseButtonUp(0) ) {
    3.  
    4.     // get both cameras
    5.     Camera display1Camera = GameObject.Find( "Display1Camera" ).GetComponent<Camera>();
    6.     Camera display2Camera = GameObject.Find( "Display2Camera" ).GetComponent<Camera>();
    7.     Camera displayActiveCamera = MagicFunctionThatDeterminesWhetherClickWasOnScreen1OrScreen2( Input, display1Camera, display2Camera ); // This is what I am missing
    8.  
    9.     // create a ray from the active camera to the click position
    10.     Ray ray = displayActiveCamera.ScreenPointToRay( Input.mousePosition );
    11.  
    12.     // check if the ray hit anything
    13.     if( Physics.Raycast( ray, out hit, touchInputMask) ) {
    14.        
    15.         // get the game object that was hit
    16.         GameObject recipient = hit.transform.gameObject;
    17.  
    18.         // interact with it
    19.         if( Input.GetMouseButtonDown(0) ) {
    20.             recipient.SendMessage( "OnTouchDown", hit.point, SendMessageOptions.DontRequireReceiver );
    21.         }
    22.  
    23.     }
    24.  
    25. }
     
  2. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    do you run one app that is split or stretched over two screens? If it is one app why not ask for the position in resolution, if greater than resolution/2, it’s right or whatever needed.
     
  3. ben4d85

    ben4d85

    Joined:
    Dec 26, 2018
    Posts:
    47
    Yes, this is one app that is using two screens. I am using two separate cameras, one with Target Display 1, and the other with Target Display 2.

    Input.mousePosition
    is returned relative to the current screen, i.e. something along the lines of
    if( input.mousePosition.x > screen1.width) { currentScreen = 2; }
    will not work because the x position returned by
    Input.mousePosition
    will never be greater than the width of the screen.

    I got it working for mouse click based on the approach discussed in https://answers.unity.com/questions/1349602/detect-the-display-of-current-mouse-position-windo.html and https://stackoverflow.com/questions...le-display-click-works-only-on-maincamera-tag.

    However, I still need to get it working for Touch events, i.e. what screen did the touch occur on?
     
    Last edited: Apr 13, 2019
  4. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    When you raycast on touch you could check for an transparent blocking object and get the camera. Not sure if the eventsystem itself holds an option for detecting the current „touched“ camera