Skip to content

Instantly share code, notes, and snippets.

@karljj1
Last active December 2, 2022 20:33
Show Gist options
  • Save karljj1/5acdb448b21fa570e8b4c1a77da8d787 to your computer and use it in GitHub Desktop.
Save karljj1/5acdb448b21fa570e8b4c1a77da8d787 to your computer and use it in GitHub Desktop.
Multiple Display Ray casting example
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