Basically, the documentation for the IARFrame.HitTest function does not satisfy my need for details. It doesn’t really explain anything about how it works, what the different result types actually mean, or how it’s intended to be used.
My biggest and most important question (the reason for this post) is why I would want to use IARFrame.HitTest instead of a basic Physics.Raycast (with a collider attached to the plane prefab of course).
Purely surface level testing has shown me next to no functional difference between the two, but a physics raycast feels smoother. It also seems more reliable on older phones.
By far, the biggest advantage of using a physics raycast is that it will actually provide a reference to the plane object, which I feel is a major oversight for the ar hittest.
Does anyone know of a reason or use case where the ar hittest would actually be better? Does it even provide anything that a physics raycast wouldn’t?
My understanding is that ARDK mesh geometry is made up of vertices and faces and they may or may not contain colliders. Physics.Raycast needs colliders to work so applying it to the ARDK’s meshes will not work.
That’s why Niantic has produced its own raycast method which they call HitTest.
This question is specifically about plane detection, not meshing. But in either case you can very easily add a collider (as I even said in my original post). They even provide a collider prefab for the realtime meshing.
Also in the original post I mentioned having used Physics.Raycast and it working fine, and sometimes even better. I think you may need to re-read the post.
As you’ve noted, there are a few differences between using the IARHitTestResult method of discerning the environment versus using Unity’s Physics.Raycast() method to test against detected planes that have been added to the scene.
For starters, the hit test method checks against the device’s underlying representation of the scene and doesn’t rely on planes generated by an ARPlaneManager component. Therefore, there’s fewer GameObjects created and less overhead managed by Unity’s internal garbage collector, which may become relevant depending on the scale of your application’s use-case.
Secondly, you’re able to access a tapped IARAnchor property (documentation) which itself contains a Matrix4x4 Transform property in case you require information to orient a spawned object with respect to the tapped object. However, you are correct that the hit test does not return a reference to the GameObject that gets tapped. The hit test method operates at a lower level under the hood which is intentionally Unity-agnostic, so capturing a reference to a GameObject this way is not an intended use-case.
Ultimately, both methods are useful in different regards and for different purposes. If you require a reference to a Unity object, then using Unity’s Physics.Raycast() will do the trick. On the other hand, if you find yourself performing a multitude of tests over a limited period of time, then the hit test result method may be more performant under those circumstances.
This is the kind of detail that I’d want in the documentation if possible. Even a simplified note about the fact that HitTest runs at a lower level and is more performant would be great.
I also want to note that while it makes sense that HitTest would be more performant, I’ve noticed it’s significantly less reliable on older devices. On my Note 10+, it’s basically unusable for single checks, but is somewhat usable if checked every frame. The Physics.Raycast route made my application so much smoother and more tolerable in all situations.