Last active
December 2, 2022 20:33
-
-
Save karljj1/5acdb448b21fa570e8b4c1a77da8d787 to your computer and use it in GitHub Desktop.
Multiple Display Ray casting example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.UI; | |
public class SelectorScr : MonoBehaviour | |
{ | |
public Camera myCam; | |
private RaycastHit hit; | |
private Ray ray; | |
public Text selectedGOName; | |
private void Update() | |
{ | |
if (Input.GetMouseButtonDown(0)) | |
{ | |
#if UNITY_EDITOR | |
var mousePosInScreenCoords = Input.mousePosition; | |
#else | |
// Convert the global mouse position into a relative position for the current display | |
var mousePosInScreenCoords = Display.RelativeMouseAt(Input.mousePosition); | |
#endif | |
// z is the display Id | |
if (mousePosInScreenCoords.z == myCam.targetDisplay) | |
{ | |
ray = myCam.ScreenPointToRay(mousePosInScreenCoords); | |
if (Physics.Raycast(ray, out hit)) | |
{ | |
hit.collider.gameObject.SetActive(false); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment