Search code examples
unity-game-enginebuttoncollision-detectionaugmented-realityarcore

ARCore cannot detect a button hit inside the canvas


Well i have to create this augmented reality application that has a floating menu inside the main screen. I'm using the ARCore SDK from Google with Unity. Until now i have managed to create my menu and set up the ARCore camera so i can view the menu correctly while the background is rendered with the device's camera. The button therefore cannot be clicked, i have tried using RayCasting onto the button with a collider but that does not work either. I'm quite new to Unity game development, thank you.

The code that i tried is this, it works with game objects but not UI Elements.

//Code from Google's example
if (Input.touchCount< 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began)
{
    return;
}
//My Code
Debug.Log("Touched");


Ray ray = FirstPersonCamera.ScreenPointToRay(Input.GetTouch(0).position);
RaycastHit hitObject;
if(Physics.Raycast(ray,out hitObject, Mathf.Infinity))
{
    Debug.Log(hitObject.transform.name);
    Debug.Log(hitObject.collider.name);
    return;
}

Solution

  • Ok, so it seems like the answer was very simple. The reason why my buttons and generally the UI was not interactive was because i didn't see the option Event Camera on the canvas. Once i added the First Person Camera from AR Core in the canvas Event Camera everything began to work. Canvas setting picture