Unity ARCore touch Object

Clash Royale CLAN TAG#URR8PPP
Unity ARCore touch Object
I am currently trying, to detect if a touch input hits a 3D Object in my ARCore scene. I simply edited the HelloAR sample script, to use a custom prefab instead of the andy object, and after i have spawned it, i want to be able to touch it. The prefab consists of six 3D platforms, each with a different name and a box collider. The following code gives very weird results.
if (Frame.Raycast(touch.position.x, touch.position.y, raycastFilter, out hit))
// Use hit pose and camera pose to check if hittest is from the
// back of the plane, if it is, no need to create the anchor.
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
if (!IsPointerOverUIObject())
if(!platsSpawned)
// Instantiate platforms at the hit pose.
var platforms = Instantiate(platformPrefab, new Vector3(hit.Pose.position.x, hit.Pose.position.y + offsetY, hit.Pose.position.z), hit.Pose.rotation);
// Create an anchor to allow ARCore to track the hitpoint as understanding of the physical
// world evolves.
var anchor = hit.Trackable.CreateAnchor(hit.Pose);
// Make platforms a child of the anchor.
platforms.transform.parent = anchor.transform;
platsSpawned = true;
else if (platsSpawned)
//Ray raycast = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
Ray raycast = FirstPersonCamera.ScreenPointToRay(Input.GetTouch(0).position);
RaycastHit raycastHit;
if (Physics.Raycast(raycast, out raycastHit))
try
debugLabel.text = raycastHit.collider.name;
var obj = GameObject.Find(raycastHit.collider.name);
obj.GetComponent<SomeScript>().DoeSmthng();
catch (Exception ex)
Debug.Log(ex.Message);
Running this code, it will sometimes not hit any platform (even though you are clearly touching them), sometimes it will detect the touch correctly and sometimes it will just hit any of the other platforms in the prefab (even when only one is visible at the moment).
I feel like there is something weird going on with the Raycast. I tried casting with "Camera.main" and "FirstPersonCamera" but the results are pretty much the same.
Any ideas on why this is happening? Does someone have a proper example or can help me to correct my code ?
EDIT: I found out that the RayCasts work as long as the platform Objects are not children of the anchor. I that maybe the Objects might be turned into "Trackables", but i am not sure how i would work with those.
So you suggest, that i am allready touching the screen somewhere else when i try to touch the object? That could be possible because of infinity Display. How would you suggest to do it then?
– Fabian P.
Aug 7 at 6:56
I'm not sure.try Input.multiTouchEnabled = false. And take a test.
– Ron Tang
Aug 7 at 6:59
I will try that in the evening. I'll let u know the result
– Fabian P.
Aug 7 at 7:34
Ok so i tried it now and it didn't solve the problem. But i found out, that the problem only occurs when the platform object is a child of the anchor. Does anyone know, how i detect Objects that are children of anchors?
– Fabian P.
Aug 7 at 13:27
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Maybe due to multitouch.Input.GetTouch(0).position maybe not your need.I guess.
– Ron Tang
Aug 7 at 6:21