Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Unable to detect plane Indicator

Discussion in 'AR' started by MNazzal, Apr 10, 2023.

  1. MNazzal

    MNazzal

    Joined:
    Sep 10, 2020
    Posts:
    18
    Hello everyone,
    Hope this message finds you well,

    I have a problem with my Augmented reality app, I'm unable to see my placement indicator

    Here's my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.XR.ARFoundation;
    5. using UnityEngine.Experimental.XR;
    6. using System;
    7. using UnityEngine.XR.ARSubsystems;
    8.  
    9. public class ARTapToPlaceObject : MonoBehaviour
    10. {
    11.     public GameObject objectToPlace;
    12.     public GameObject placementIndicator;
    13.     private ARSessionOrigin arOrigin;
    14.     private Pose placementPose;
    15.     private bool placementPoseIsValid = false;
    16.     private ARRaycastManager raycastManager;
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         arOrigin = FindObjectOfType<ARSessionOrigin>();
    21.         raycastManager = FindObjectOfType<ARRaycastManager>();
    22.  
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void Update()
    27.     {
    28.         UpdatePlacementPose();
    29.         UpdatePlacementIndicator();
    30.  
    31.         if(placementPoseIsValid && Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) {
    32.             PlaceObject();
    33.         }
    34.     }
    35.  
    36.     private void PlaceObject()
    37.     {
    38.         Instantiate(objectToPlace, placementPose.position, placementPose.rotation);
    39.     }
    40.  
    41.     private void UpdatePlacementIndicator()
    42.     {
    43.         if(placementPoseIsValid)
    44.         {
    45.             placementIndicator.SetActive(true);
    46.             placementIndicator.transform.SetPositionAndRotation(placementPose.position, placementPose.rotation);
    47.         }
    48.         else
    49.         {
    50.             placementIndicator.SetActive(false);
    51.         }
    52.     }
    53.  
    54.     private void UpdatePlacementPose()
    55.     {
    56.         raycastManager = FindObjectOfType<ARRaycastManager>();
    57.         var screenCenter = Camera.current.ViewportToScreenPoint(new Vector3(0.5f, 0.5f));
    58.         var hits = new List<ARRaycastHit>();
    59.         raycastManager.Raycast(screenCenter, hits, TrackableType.Planes);
    60.  
    61.         placementPoseIsValid = hits.Count > 0;
    62.  
    63.         if(placementPoseIsValid)
    64.         {
    65.             placementPose = hits[0].pose;
    66.  
    67.             var cameraForward = Camera.current.transform.forward;
    68.             var cameraBearing = new Vector3(cameraForward.x, 0, cameraForward.z).normalized;
    69.             placementPose.rotation = Quaternion.LookRotation(cameraBearing);
    70.         }
    71.     }
    72. }
    73.  
    And here's my scene images



    Kind regards,
     
  2. MNazzal

    MNazzal

    Joined:
    Sep 10, 2020
    Posts:
    18
    Any solution?
     
  3. mujahid077

    mujahid077

    Joined:
    Apr 11, 2023
    Posts:
    2
    There doesn't seem to be any obvious problems with the code that you provided that would cause the placement indicator to not appear. However, it's possible that the issue could be caused by something outside of this script, such as the configuration of the ARSession, the lighting conditions in your environment, or the size of the object that you're trying to place.

    Here are a few things you can check:

    1. Make sure that the placementIndicator object is assigned in the inspector, and that it's set to be active by default.

    2. Check that the placementIndicator object is not being blocked by another object in the scene. Try moving the camera around to see if the placement indicator appears from a different angle.

    3. Ensure that there are surfaces in the camera view that can be detected by the AR system. Try moving the camera around to different areas and see if the placement indicator appears in a different location.

    4. Check if there are any errors or warnings in the console that could be related to the issue.

    5. Try increasing the size of the placementIndicator object to make it more visible.
    If none of these suggestions work, you may want to try creating a new, simple AR project with just the placement indicator and see if it appears correctly. This can help isolate the issue and determine if it's related to the code or something else.
     
    MNazzal likes this.
  4. MNazzal

    MNazzal

    Joined:
    Sep 10, 2020
    Posts:
    18
    @mujahid077 Thank you for your response,
    None of the above helped me,
    I have figured it out by adding a new component to the ARSessionOrigin "AR Plane Manager" and by this it has been fixed :)
     
    mujahid077 likes this.
  5. mujahid077

    mujahid077

    Joined:
    Apr 11, 2023
    Posts:
    2
    I'm glad to hear that you were able to resolve the issue by adding the "AR Plane Manager" component to the ARSessionOrigin. This component is responsible for detecting flat surfaces in the camera view and creating ARPlane objects in the scene.

    Adding this component can be helpful if your app requires the user to place objects on a flat surface, as it allows the app to detect and display the surface in real-time.
     
    andyb-unity and MNazzal like this.