ARRaycastManager Raycast not working

Unity Editor version : 2022.2.21f1
MLSDK version : v1.3.0-dev2
ML Hub version : 2.0.12.202306052102
Magic Leap App Simulator for Unity : 3.6.0-187.zi3b19841
Host OS : Windows
Host RAM: 16GB
Host Processor: i7 10th gen
Host GPU: Nvidia Mx250
ML Hub Version 2.0.12.202306052102
Magic Leap Application Simulator Module Version 3.6.0.202306092358
Magic Leap Application Simulator Runtime Version 3.6.0.20230609
Server: 3.6 (built Jun 9 2023), Protocol: 19
ZIF (bundled): Version 0.0.34.20230609
ZIF (runtime): Version 0.0.34.20230609

I am trying to build on top of the Plane example scene provided. The plane detection is working fine and I am able to see the planes. But when I am trying to raycast to the planes using the ARRaycastManager.Raycast function it is always returning false.
I am using the following code to perform the raycast.

 Vector3 controllerPosition = controllerActions.Position.ReadValue<Vector3>();
        Vector3 controllerDirection = Vector3.Normalize(controllerActions.Rotation.ReadValue<Quaternion>() * Vector3.forward);
      
        Ray controllerRay = new Ray(controllerPosition, controllerDirection);
        //Raycast
        if (arRaycastManager.Raycast(controllerRay,hits,TrackableType.PlaneWithinPolygon))
        {
           Debug.Log("raycast success");
        }
        else { Debug.Log("raycast false");
        }

I also tried using the Physics.raycast it works but it gives a offset from the actual hit position.

Are you able to visualize the ray? This may help make debugging a bit easier.

Yes, I also tried to visualize the ray, and it appears in the correct place and direction, but the raycast is not working.

Hi @asingh9829,

For raycasting, we recommend using Physics.Raycast(). Here is a code snippet from the Magic Leap 2 Prototypes Project:

private void Update()
    {
        if (planeManager.enabled)
        {
            
            PlanesSubsystem.Extensions.Query = new PlanesSubsystem.Extensions.PlanesQuery
            {
                Flags = planeManager.requestedDetectionMode.ToMLQueryFlags() | PlanesSubsystem.Extensions.MLPlanesQueryFlags.Polygons | PlanesSubsystem.Extensions.MLPlanesQueryFlags.Semantic_Wall,
                BoundsCenter = Camera.main.transform.position,
                BoundsRotation = Camera.main.transform.rotation,
                BoundsExtents = Vector3.one * 20f,
                MaxResults = maxResults,
                //MinHoleLength = minHoleLength,
                MinPlaneArea = minPlaneArea
            };
        }

        Ray raycastRay = new Ray(controllerActions.Position.ReadValue<Vector3>(), controllerActions.Rotation.ReadValue<Quaternion>() * Vector3.forward);
        if (isPlacing & Physics.Raycast(raycastRay, out RaycastHit hitInfo, 100, LayerMask.GetMask("Planes")))
        {
            //Debug.Log(hitInfo.transform);
            mediaPlayerIndicator.transform.position = hitInfo.point;
            mediaPlayerIndicator.transform.rotation = Quaternion.LookRotation(-hitInfo.normal);
            mediaPlayerIndicator.gameObject.SetActive(true);

        }
    }

Let me know if this helps.

Best,

El

Hi,
Thanks for the reply.
But on the Magic Leap 2 documentation page it says to use the ARRaycast Manager from the ARFoundation package.

This is what is written on the documentation page for magic leap 2.

Ray casting, also known as hit testing, allows you to determine where a ray (defined by an origin and direction) intersects with a trackable. Traditionally, developers use Unity's physics module but since AR Planes are not always present in the physical world, AR Foundation provides a separate interface.

The raycast manager serves two purposes:

Provides an API to perform single raycasts. Allows you to create a persistent ARRaycast. An ARRaycast is a type of trackable and is updated automatically until you remove it. Conceptually, it is similar to repeating the same raycast query each frame, but platforms with direct support for this feature can provide better results.

Link to documentation page

Thank you for bringing this to our attention and apologies for the misleading information. We are updating our documentation to accurately represent the best practices.

Best,

El