google-ar / arcore-unity-sdk

ARCore SDK for Unity
https://developers.google.com/ar
Other
1.4k stars 400 forks source link

TrackableHit doesn't have a game object #328

Closed idomic closed 6 years ago

idomic commented 6 years ago

Hi,

Ever since you've changed the SDK version I can't really extract the gameobject I'm touching. Before the upgrade the intersection was of RaycastHit so I could get the intersected gameobject and it's collider but Now I really can't get it due to the inner implementation of the TrackableHit.

How can I get both the intersected gameobject and the object's collider?

Thanks!

pablisho commented 6 years ago

Hi, Frame.Raycast is for checking raycast with ARCore objects (Planes, Feature points), not for checking collision with Unity gameobjects with colliders. For that you should use the standard Physics.Raycast. Does that help?

idomic commented 6 years ago

Hi Pablo,

Thanks for your quick reply. I'll try to explain myself better.

What was happening in the ARCore v 1.0 was that if there was a Physics.Raycast intersection I could take the object the ray was intersected with. Now on the example code of HelloAR, there's a check you guys are doing:

// Raycast against the location the player touched to search for planes. TrackableHit hit; TrackableHitFlags raycastFilter = TrackableHitFlags.PlaneWithinPolygon | TrackableHitFlags.FeaturePointWithSurfaceNormal; if ((hit.Trackable is DetectedPlane) && Vector3.Dot(FirstPersonCamera.transform.position - hit.Pose.position, hit.Pose.rotation * Vector3.up) < 0) { Debug.Log("Hit at back of the current DetectedPlane"); } else {

Now what I actually want is to see first if there's an intersection at all with some collider and then check intersection with a cube I created over there. I've used to do it like you said in Physics.Raycast as you guys were using those checks but now there are those new objects.

Is there any way to reuse those new objects or should I check for Physics.Raycast the same way it used to be and ignore the new objects?

Thanks!

pablisho commented 6 years ago

Hi, TrackableHit is to detect hits with ARCore Trackables, it won't work with your standard Unity game objects. That code in the example is not changing its behaviour. You should use Physics.Raycast to raycast against Unity gameobjects with colliders.

idomic commented 6 years ago

Thanks Pablo,

So I'll maintain another set of variables for raycasting the game objects. I'll let you know how if I have more trouble with this issue.

Also, I've noticed that when I don't attach an anchor to the cubes it actually falls through the detected planes, it's because of the missing attachment right? Shouldn't the collider help in this case?

pablisho commented 6 years ago

Adding an anchor is the right way to place objects in AR with ARCore. If you don't do it, you'll see that the objects don't stay fixed in the right positions. Colliders won't help with ARCore tracking to pose them in the right way.

idomic commented 6 years ago

So I've tried to use the standard Physics.Raycast intersection with the touch point I get:

Ray hitInfo = FirstPersonCamera.ScreenPointToRay(new Vector3(touch.position.x, touch.position.y, 0));
                        RaycastHit intersection;
                        if (Physics.Raycast(hitInfo, out intersection))
                        {
Debug.Log("There's intersection");
}

But it doesn't recognise the intersection with the plane what made me think there is no collider attached to the plane? I also don't understand in what situation will someone touch the back of the plane? If someone is touching outside of the plane this touch is being ignored right?

Do I have to maintain 2 rays, once for intersection with the plane like in the example and one for object detection with regular raycasting?

Thanks!

idomic commented 6 years ago

I'm also having some difficulties with the anchor - Shouldn't I attach it after the object is set like this:

GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
                            cube.transform.position = 
FirstPersonCamera.transform.TransformPoint(hit.Pose.position);
                            var anchor = hit.Trackable.CreateAnchor(hit.Pose);
                            // Make Andy model a child of the anchor.
                            cube.transform.parent = anchor.transform;

Is it related to the fact I don't keep a prefab like the Andy example?

                            // Instantiate Andy model at the hit pose.
                            //var andyObject = Instantiate(AndyAndroidPrefab, hit.Pose.position, hit.Pose.rotation);
small-potatoes commented 6 years ago

Hi idomic,

Do I have to maintain 2 rays, once for intersection with the plane like in the example and one for object detection with regular raycasting?

You can use a single ray but you will pass it to calls of GoogleARCore.Session.Raycast for raycasting against ARCore trackables and Physics.Raycast for raycasting against objects (with colliders) in your game scene.