Multiple Display Raycast problem
I've an issue with raycasting multi display. I have three displays (and cameras) on three different monitors, and i need to raycast all monitors for clicking different object. I use this script, but work only on One monitors, exactly which that i add first on cameras array. Other script works only on MainCamera. So i don't know how resolve.
Anybody can help please?
This is my code and a screen of Cameras.
Code:
[ DllImport("user32.dll") ]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetCursorPos(out MousePosition lpMousePosition);
[StructLayout(LayoutKind.Sequential)]
public struct MousePosition
{
public int x;
public int y;
}
private static int GetHoveredDisplay()
{
// Get the absolute mouse coordinates
MousePosition mp;
GetCursorPos(out mp);
// Get the relative mouse coordinates
Vector3 r = Display.RelativeMouseAt(new Vector3(mp.x, mp.y));
// Use the z coordinate
int displayIndex = (int)r.z;
return displayIndex;
}
void Update()
{
RaycastHit hit;
if (Input.GetMouseButtonDown(0))
{
int di = GetHoveredDisplay();
instruction.text = di.ToString();
Camera currentDisplayCamera = cameras[di];
Ray ray = currentDisplayCamera.ScreenPointToRay(Input.mousePosition);
Debug.Log(currentDisplayCamera);
if (Physics.Raycast(ray, out hit))
{
hit.transform.root.GetComponent<Animator>().speed = 0f;
GameObject ChildGameObject1 = hit.transform.GetChild(0).gameObject;
GameObject ChildGameObject2 = ChildGameObject1.transform.GetChild(0).gameObject;
ChildGameObject2.GetComponent<Animator>().SetBool("prova", true);
StartCoroutine(Activation(hit));
}
}
}
This is a screen:
Your answer
Follow this Question
Related Questions
Physics.Raycast ignores instantiated objects. 1 Answer
Get click position on gameobject from OnPointerClicks PointerEventData 1 Answer
Raycast on Graphic with Custom Mesh, 1 Answer
I never get inside if (Physics.Raycast(ray, out hit,Mathf.Infinity, touchInputMask) 1 Answer
Unity 5 - My raycast is hitting my player, and I can't figure out why 2 Answers