Raycasting objects by its Tag with ARFoundation

Hi guys.

I’m creating an AR game where I need to tap over a 3D Object on my scene an shoot to its position. That’s the essentially my problem.

Yes, as I understand from ARFoundation Documentation, you can only Raycast Planes. I mean, currently, you can’t detect other 3D objects in the scene. Please, if I’m wrong, do you have an example in code of how to detect an object by its tag? as it’s on Raycasting out of AR.

Now I’m pretty sure that by now there must be some people requiring to make a Raycast over a 3D object (not a plane). Probably they bend the rules. Do you have an example of it that you may share? What I’m looking for is to Raycasting Detect an object by its tag using ARFoundation.

A simple code of Raycasting over 3D is something like this:

    void Update()
    {
        RaycastHit hit;
        if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity))
        {
            Debug.Log("Raycasting Detected: " + hit.collider.tag);
            if (hit.collider.tag == "DroneStoreItem")
            {
                GameObject droneStoreItem = hit.collider.gameObject;

                if (droneStoreItem.GetComponent<DroneStoreItem>() != null)
                {
                    droneStoreItem.GetComponent<DroneStoreItem>().DroneStoreBoxTouch();
                }
            }
        }
    }

Now, how can I translate previous code using ARSessionOrigin, Pose and a TrackableType (NOT a Plane or Feature Point).

I’d really appreciate any help.

Hi
Could you use the IPointerClickHandler instead of raycasting?

https://docs.unity3d.com/ScriptReference/EventSystems.IPointerClickHandler.OnPointerClick.html

Thanks for your suggestion. But, as I understand, IPointer only detects clicks with the mouse, not tap over an AR Scene. Please, if I’m wrong, do you have an example code of what you are suggesting?

As far as I am aware it should work for mobile… the Enter/Exit events might not… but the click should.

There are some requirements… such as a Physics Raycaster attached to cam and an Event System in the scene.

https://docs.unity3d.com/ScriptReference/EventSystems.IPointerClickHandler.html

https://docs.unity3d.com/Manual/SupportedEvents.html

Worth a try… Is there a reason you can’t use the normal Physics Raycasting in the AR scene otherwise?

Your question about why don’t use Physics Raycasting in the AR, is really good. I haven’t consider that. Let me try it.

THANK YOU greenmachine. Your suggestion as simple as it was, got right in the center. It worked perfectly. Sometimes we entangle ourselves in difficult solutions when the obvious answer is in front of you. Thank you for giving me a new and simple perspective.

4 Likes

It sounds like you’ve found an answer that suits you, but I was curious about your question:

The AR raycast only raycasts against things that have been detected by the device. Currently, handheld AR (ARCore & ARKit) only detect feature points (points in the point cloud) and planes. Are you trying to use the AR raycast as a general raycaster for (virtual) content in your scene?

Hi @tdmowrer . Yes, that was exactly what I was trying to do. I need to tap over virtual elements on an AR game.

1 Like

Ah okay. Unfortunately, that isn’t what the AR raycaster is for. Sounds like you’ve got a solution that works for you :slight_smile:

Yes. I really think that ARFoundation is awesome, but it needs a lot more documentation and examples. We’re working on several projects with it but we had spent a lot of time trying to understand simple things. I hope you have that in consideration for next upgrades.
Thanks for your support.

For what would you like to see more samples and documentation? Both manual and full scripting API documentation is here – let me know what’s missing :slight_smile:

1 Like

When you launched the experimental-AR Interface, you created a lot of different cool examples. What I suggest is to have those same examples but for this official release.

Thank you for the documentation. By now, I almost know it by memory. But nothing like an example.

A couple of things that have come up often on this forum, and would benefit from an example for those new to ARFoundation:

Disabling plane detection and cleaning out detected planes after you have placed your content
FocusSquare/UI while scanning for planes

In the absence of an ARFoundation editor toggle to turn on/off vertical plane detection

Example of how to ignore vertical planes

1 Like

Thanks for the feedback. I’ve added two new scenes to the samples repo which demonstrate

  • Toggle plane detection and existing planes
  • Selectively disable vertical planes

The FocusSquare UI is also a good suggestion. I’ll look into that too.

Great thanks.
Let us know if/when you commit a FocusSquare sample.

1 Like

+1
An example of this would be absolutely awesome

Hello tonOnwu I really need to know the details how above solution worked for you?As I am new to unity and I am working on a project I need to submit really soon!!Thx for any help beforehand

I want to Raycast to point cloud. How to acheive it?
please see this thread https://forum.unity.com/threads/how-to-raycast-to-point-and-cloud-particle-in-arfoundation.753812/#post-5019614

Your code computes a Ray

Ray ray = Camera.main.ScreenPointToRay(touch.position);
raycastManager.Raycast(ray, hits, TrackableType.FeaturePoint);

which is okay, but unnecessary. The Raycast interface accepts screen points too.

I’m not sure what you mean by “wrong results”, but I noticed you aren’t checking whether the raycast actually hits anything, so I imagine it’s probably throwing an IndexOutOfRangeException when you unguardedly access hits[0].

Can you be more specific about what doesn’t work?

Thanks for the reply. I made it working. The issue is the feature point pose is in local space. I converted it into world space and everything works fine.