I have 4 rectangles of identical size (each in every quadrant of screen). I created them programatically in onStart() method:
Vector3 pos1 = new Vector3(Screen.width / 4, Screen.height / 4, 0);
Vector2 rectDimensions = new Vector2(Screen.width / 2, Screen.height / 2);
GameObject bottomLeftGO = new GameObject();
bottomLeftGO.transform.SetParent(mainCanvas.transform);
Image bottomLeftRect = bottomLeftGO.AddComponent<Image>();
bottomLeftRect.color = new Color(0.192F, 0.051F, 0.125F);
bottomLeftRect.rectTransform.sizeDelta = rectDimensions;
bottomLeftGO.transform.position = pos1;
Currently, screen looks like this:
mainCanvas is a prefab I created in Unity GUI, and connected it to the script in which I made rectangles.
I want to detect touches on the rectangles in Update() method. I tried using Raycast like this:
if (Input.touchCount > 0)
{
foreach (Touch touch in Input.touches)
{
Ray ray = Camera.main.ScreenPointToRay(touch.position);
if (Physics.Raycast(ray, out hit, touchInputMask))
{
GameObject hittedObject = hit.transform.gameObject;
}
}
}
But the condition if (Physics.Raycast(ray, out hit)) always returns false. I tried it using Input.GetMouseButton(0) so I do not need to build .apk on phone, but it still fails to detect any hits.
I am fresh new to Unity and do not understand all the concepts, so If anyone has an idea what could I try or has the solution I would be more than glad to listen. Thank you.