Search Unity

Question AR Raycast Manager will not hit a tracked AR Plane.

Discussion in 'AR' started by Mohx-games, May 31, 2023.

  1. Mohx-games

    Mohx-games

    Joined:
    Feb 17, 2020
    Posts:
    8
    Hello.

    I am in dire need of help. I have made many AR projects in the past and have done this exact same behaviour tens of times. I cannot get raycasting to work this time.

    To start, let's analyze this extremely simple code:

    Code (CSharp):
    1. Vector3 screenCenter = new Vector3(Screen.width / 2f, Screen.height / 2f, 0f);
    2. Ray ray = Camera.main.ScreenPointToRay(screenCenter);
    3.  
    4. print("Raycast Manager is enabled: " + raycastManager.isActiveAndEnabled);
    5. print("Main camera is: " + Camera.main.gameObject.name);
    6. print("Raycast hit list is not null: " + (hits != null));
    7. print("Anchor is locked: " + anchorLocked);
    8.  
    9. if (raycastManager.Raycast(ray, hits, TrackableType.AllTypes) && !anchorLocked)
    10. {
    11.      print("Ray hit plane");
    12. }
    13.  
    Quite simply, this never will print 'Ray hit plane'.

    To elaborate, let's consider some potential issues, and why they are not the cause:
    1) The print statements above return normal values. The raycast manager is enabled, the camera being used is the main AR Camera, the raycast hit list is not null, and the anchorLocked boolean is false.
    2) I have also attempted to use the camera's position and forward vector when creating a new ray, this also does not work. The intention is to move an object along the plane in the center of the phone screen.
    3) The ARPlane is the default prefab Unity provides, the collider is on. The sole change is the layer, which is set to a custom layer. Changing it back to default doesn't change anything.
    4) I have tried every TrackableType that the enum provides, none of them work.

    For some additional information:
    Unity 2021.3.15f1
    ARFoundation 4.2.7
    Galaxy S21 being built upon
    ARCore selected as the subsystem provider


    Any help is appreciated, I am completely lost as to why this doesn't work.

    Thank you.

    EDIT: As it should be obvious, no, no errors are ever thrown.
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,000
    That's odd because even if a plane isn't hit you should at least expect a feature point to be hit. I suggest you figure out why you're not getting any hits at all before worrying about planes.

    Make sure your AR Camera is tagged 'MainCamera'.

    ..and try this raycast:

    Code (CSharp):
    1. if (raycastManager.Raycast(new Vector2(Screen.width/2,Screen.height/2), hits,TrackableType.FeaturePoint))
     
  3. andyb-unity

    andyb-unity

    Unity Technologies

    Joined:
    Feb 10, 2022
    Posts:
    1,062
  4. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,000
    TrackableType.AllTypes should be renamed to AllTypesExceptPlanesAndMaybeSomeOtherStuff

    :)

    Mohxgalleries, try something like this:

    Code (CSharp):
    1.         if (raycastManager.Raycast(new Vector2(Screen.width/2,Screen.height/2), hits,TrackableType.PlaneWithinPolygon))
    2.         {
    3.             var hitPose = hits[0].pose;
    4.             var hitTrackableId = hits[0].trackableId;
    5.             var hitPlane = m_PlaneManager.GetPlane(hitTrackableId);
    6.             anchor = m_AnchorManager.AttachAnchor(hitPlane, hitPose);
    7.             GameObject obj=Instantiate(prefab, anchor.transform);
    8.         }
    9.  
     
  5. andyb-unity

    andyb-unity

    Unity Technologies

    Joined:
    Feb 10, 2022
    Posts:
    1,062