Hey, quick question. Has anyone had any luck with detecting an object as of yet?
In the HelloWorld example, you can access the out hit param as you would with a normal raycast. But it has no information entailing what game object it hit. For example hit.trasform.gameobject.tag
I have tried to use Physics.Raycast on Andy object from HelloArExample, but it is not hitting anything. Below is my script.
Ray raycast = m_firstPersonCamera.ScreenPointToRay(touch.position);
RaycastHit raycastHit;
if (Physics.Raycast(raycast, out raycastHit))
{
Debug.LogError("Something Hit");
if (raycastHit.collider.name == "Andy")
{
Debug.LogError("andy clicked");
}
}else{
Debug.LogError("No hit detected");
}
So, I have edited HelloARController.cs script. Here m_firstPersonCamera is the ARCamera used by Google ARCore, Even though I click on Andy object, it always goes to the else part of first if condition.
OK, I have finally figured out why Physics.Raycast not working correctly with andy object. It is by correctly setting box collider. Below are the correct steps to make a ray hit the andy object.
Add box collider component to andy prefab
set center of the box collider as: x:0, y:0.1, z:0
set size of the box collider as: x:0.15,y:0.2,z:0.1
Or you can set this values visually by drag and drop andy prefab to unity scene editor.
GameObjects need to have colliders in order for raycast to see them, even in ARCore.
Physics.Raycast out hit and then hit.collider.gameObject can be used to access the GameObject.