Disclaimer: I'm pretty new to Unity3D and ARCore, so please bear with me.
I'm using ARCore in Unity3D to create a scene where the user can select models in a ScrollView
on the screen and place them using Google's ARCore framework.
So far I have it working; the user touches a model in the ScrollView
(which is indicated on-screen in a Panel
as the currently selected model, as my plans are to have the ScrollView
toggle visibility for more screen view space).
The problem is that when a user selects a model, ARCore is placing a model on the detected plane behind where the ScrollView
and selected model Panel
objects are (even when you first touch to start scrolling the ScrollView
). See below to help visualize.
How can I get ARCore to not place the object behind the ScrollView
and Panel
? What I've tried is adding to my controller (which is really just the Google HelloARController) a collection of objects that I want to block ARCore's Raycast
and iterate through them with a foreach
to see if the Raycast
hits the GameObjects
in the collection
Touch touch;
if (Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began)
{
return;
}
//my code; above is Google's
foreach (var item in BlockingObjects) { //BlockingObjects is a List<GameObject>
if (IsTouchInObject(FirstPersonCamera.ScreenPointToRay(touch.position), item)) {
return;
}
}
//end of my code; below is Google's
TrackableHit hit;
TrackableHitFlag raycastFilter = TrackableHitFlag.PlaneWithinBounds | TrackableHitFlag.PlaneWithinPolygon;
With the IsTouchInObject
function defined like this:
private bool IsTouchInObject(Ray ray, GameObject obj) {
RaycastHit rch;
Physics.Raycast (ray, out rch);
return (rch.collider != null);
}
The thing that is failing is that rch.collider
is always null
(I know that I'm not testing against the object at all, I'll worry about that once I can get the Raycast
to actually collide with a GameObject
). I've tried using Physics
/Physics2D
with RaycastHit
/RacastHit2D
and attaching BoxCollider
/BoxCollider2D
components to the objects I want to detect the hit on, but nothing I've done is working.
(This solution was taken from something on the Unity3D forums in which someone had a similar issue, but not with AR, with their own 3D world with a 2D overlay. I can't find that forum post to provide reference, sorry).
Any help would be greatly appreciated.
EDIT/NOTE: I've now noticed there is a Graphic Raycaster
component on the Canvas
, which contains my ScrollView
and Panel
. I've tried setting the Blocking Objects
to Two D
(while adding a Box Collider 2D
to the ScrollView
and Panel
) and Blocking Mask
to Ignore Raycast
(and a couple other things) to no avail. Is there a combination of values for these properties that could do it?
In the spirit of today's date:
Have you tried wrapping the Raycast with:
if (!EventSystem.current.IsPointerOverGameObject(touch.fingerId)) {...}
https://answers.unity.com/questions/1406243/ui-in-arcore-helloar-example.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With