Search Unity

ARFoundation Raycast on Infinite plane

Discussion in 'AR' started by Alexis-Dev, Jan 15, 2020.

  1. Alexis-Dev

    Alexis-Dev

    Joined:
    Apr 16, 2019
    Posts:
    121
    Hello everybody,

    I try to place a gameobject on an ARPlane.
    No problem when I make a Raycast on TrackableType.PlaneWithinBounds and I click on the plane.

    But nothing happen when I use TrackableType.PlaneWithinInfinity and I click indide or outside the plane.

    Someone know how to "convert" my plane to Inifinte plane and raycast outside the bound?

    Best,
    Alexis
     
  2. Drakulo

    Drakulo

    Joined:
    Oct 31, 2012
    Posts:
    6
    Hi there. I stumbled on the same problem, here is how I did it:

    Create a Prefab with a correctly oriented Quad. Give it a big size (I used 50x50x1). Put a material with some transparency for tests. Affect this Prefab to a specific layer (like "InfiinitePlane").

    Create a script where you listen for the "planesChanged" event on the ARPlaneManager and when a plane is detected :

    Code (CSharp):
    1. private void OnPlanesChanged(ARPlanesChangedEventArgs evt)
    2. {
    3.     List<ARPlane> addedPlanes = evt.added;
    4.     if (addedPlanes.Count > 0 && m_InfinitePlane == null)
    5.     {
    6.         ARPlane plane = addedPlanes[0];
    7.         Vector3 position = plane.center;
    8.         m_InfinitePlane = Instantiate(m_InfinitePlanePrefab, position, Quaternion.identity);
    9.     }
    10. }
    Then, in the script to manage the placement of your GameObject, raycast against your custom infinite plane instead of using the ARFoundation tools with something like this:

    Code (CSharp):
    1. Ray ray = Camera.main.ScreenPointToRay(m_CurrentTouchPosition);
    2. RaycastHit hit;
    3. if (Physics.Raycast(ray, out hit, Mathf.Infinity, LayerMask.GetMask("InfinitePlane")))
    4. {
    5.     // Place your GameObject
    6. }
    Works like a charm in my project.

    Cheers!
     
  3. KyryloKuzyk

    KyryloKuzyk

    Joined:
    Nov 4, 2013
    Posts:
    1,144
  4. Alexis-Dev

    Alexis-Dev

    Joined:
    Apr 16, 2019
    Posts:
    121
    Hi,

    thanks for this tips!

    Best,
    Alexis
     
  5. KyryloKuzyk

    KyryloKuzyk

    Joined:
    Nov 4, 2013
    Posts:
    1,144