Search Unity

Question Unable to place Anchors

Discussion in 'AR' started by AnthonyMir, Dec 9, 2021.

  1. AnthonyMir

    AnthonyMir

    Joined:
    Jul 10, 2021
    Posts:
    24
    Sorry if the code isn't neat.

    I'm able to place anchors the first time I "Activate the AR" inside the app, If i go back and open AR Once again
    I'm Unable to place Anchors Why is that ?

    Thank you in Advance

    Code (CSharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using UnityEngine.UI;
    7. using UnityEngine.XR.ARFoundation;
    8. using UnityEngine.XR.ARSubsystems;
    9.  
    10. public class Positioning : MonoBehaviour
    11. {
    12.     [SerializeField]
    13.     public GameObject root;
    14.  
    15.     public string childname;
    16.  
    17.     [SerializeField]
    18.     GameObject xyz;
    19.  
    20.     private Transform child;
    21.  
    22.     Vector3[] positions = new Vector3[2];
    23.  
    24.     [SerializeField]
    25.     public Text debugLog;
    26.  
    27.     [SerializeField]
    28.     public Button toggleButton;
    29.  
    30.     public Vector3 Cube1;
    31.     public Vector3 Cube2;
    32.  
    33.     [SerializeField]
    34.     public Text referencePointCount;
    35.  
    36.     private ARRaycastManager arRaycastManager;
    37.  
    38.     private ARAnchorManager arAnchorManager;
    39.  
    40.     private ARPlaneManager arPlaneManager;
    41.  
    42.     private List<ARAnchor> referencePoints = new List<ARAnchor>();
    43.  
    44.     private static List<ARRaycastHit> hits = new List<ARRaycastHit>();
    45.     public int helper;
    46.     public void removePoints()
    47.     {
    48.         referencePoints.Clear();
    49.     }
    50.  
    51.     private void Awake()
    52.     {
    53.         child = root.transform.GetChild(0);
    54.         Debug.Log(child.name);
    55.  
    56.         arRaycastManager = GetComponent<ARRaycastManager>();
    57.         arAnchorManager = GetComponent<ARAnchorManager>();
    58.         arPlaneManager = GetComponent<ARPlaneManager>();
    59.         toggleButton.onClick.AddListener(TogglePlaneDetection);
    60.     }
    61.  
    62.     private void OnEnable()
    63.     {
    64.  
    65.         hits.Clear();
    66.         referencePoints.Clear();
    67.         referencePointCount.text = $"Reference Point Count: {referencePoints.Count}";
    68.  
    69.         if (toggleButton.GetComponentInChildren<Text>().text == "Enable Plane Detection")
    70.         {
    71.             TogglePlaneDetection();
    72.         }
    73.     }
    74.     private void OnDisable()
    75.     {
    76.         removePoints();
    77.         referencePointCount.text = $"Reference Point Count: {referencePoints.Count}";
    78.  
    79.     }
    80.     private void OnDestroy()
    81.     {
    82.         removePoints();
    83.         referencePointCount.text = $"Reference Point Count: {referencePoints.Count}";
    84.  
    85.     }
    86.  
    87.     [System.Obsolete]
    88.     void Update()
    89.     {
    90.  
    91.         if (Input.touchCount == 0)
    92.         {
    93.             return;
    94.         }
    95.  
    96.         Touch touch = Input.GetTouch(0);
    97.  
    98.         if (touch.phase != TouchPhase.Began)
    99.         {
    100.             return;
    101.         }
    102.  
    103.         if (arRaycastManager.Raycast(touch.position, hits, TrackableType.PlaneWithinPolygon))
    104.         {
    105.             if(toggleButton.GetComponentInChildren<Text>().text == "Disable Plane Detection") {
    106.  
    107.                 Pose hitPose = hits[0].pose;
    108.                 ARAnchor anchorPoint = arAnchorManager.AddAnchor(hitPose);
    109.  
    110.                 if (anchorPoint == null)
    111.                 {
    112.                     referencePointCount.text = $"Reference Point Count: {referencePoints.Count}";
    113.                 }
    114.                 else
    115.                 {
    116.                     referencePoints.Add(anchorPoint);
    117.                     referencePointCount.text = $"Reference Point Count: {referencePoints.Count}";
    118.                 }
    119.  
    120.                 positions[referencePoints.Count - 1] = anchorPoint.transform.position;
    121.  
    122.                 if (referencePoints.Count == 2)
    123.                 {
    124.                     root.SetActive(true);
    125.                     child = root.transform.GetChild(0);
    126.  
    127.                     Vector3 CubePosition = Cube1 - child.Find(childname).transform.position;
    128.  
    129.                     //Vector3 BuildingPosition = positions[0] - CubePosition;
    130.                     Vector3 BuildingPosition = positions[0]- CubePosition;
    131.                     child.Find(childname).transform.position = BuildingPosition;
    132.  
    133.                     xyz.SetActive(false);
    134.  
    135.                     /*
    136.                      *               A  --> maxBounds                       // Cube 2
    137.                      *              /
    138.                      *             /
    139.                      *            /
    140.                      *           B   --> position[0] --> MinBounds          // Cube 1
    141.                      *            \
    142.                      *             \
    143.                      *              \
    144.                      *               C  --> Position [1]
    145.                      *          
    146.                      *          
    147.                      *           Vector BA --> MaxBounds - MinBounds          
    148.                      *           Vector BC --> Position[1] - Position[0]
    149.                      *
    150.                      *           BA_Mag = Sqrt(BA^2)
    151.                      *           Vector BANorm = (BA / BA_Mag)
    152.                      *          
    153.                      *           BC_Mag = Sqrt(BC^2)
    154.                      *           Vector BCNorm = (BC / BC_Mag)
    155.                      *          
    156.                      *          
    157.                      */
    158.  
    159.                     Vector3 BA = new Vector3(Cube2.x - Cube1.x, Cube1.y, Cube2.z - Cube1.z);
    160.                     Vector3 BC = new Vector3(positions[1].x - positions[0].x, Cube1.y, positions[1].z - positions[0].z);
    161.  
    162.                     float BA_mag = Mathf.Sqrt(BA.x * BA.x + BA.y * BA.y + BA.z * BA.z);
    163.                     Vector3 BA_norm = new Vector3(BA.x / BA_mag, BA.y / BA_mag, BA.z / BA_mag);
    164.  
    165.                     float BC_mag = Mathf.Sqrt(BC.x * BC.x + BC.y * BC.y + BC.z * BC.z);
    166.                     Vector3 BC_norm = new Vector3(BC.x / BC_mag, BC.y / BC_mag, BC.z / BC_mag);
    167.  
    168.                     float res = BA_norm.x * BC_norm.x + BA_norm.y * BC_norm.y + BA_norm.z * BC_norm.z;
    169.  
    170.                     float rotationAngle = Mathf.Acos(res) * 180 / Mathf.PI;
    171.  
    172.                     Debug.Log("Vector BA " + BA);
    173.                     Debug.Log("BA_mag " + BA_mag);
    174.                     Debug.Log("BA_norm " + BA_norm);
    175.  
    176.                     Debug.Log("*********************");
    177.  
    178.                     Debug.Log("Vector BC " + BC);
    179.                     Debug.Log("BC_mag " + BC_mag);
    180.                     Debug.Log("BC_norm " + BC_norm);
    181.  
    182.                     Debug.Log("*********************");
    183.  
    184.                     Debug.Log("res " + res);
    185.                     Debug.Log("rotationAngle " + rotationAngle);
    186.  
    187.                     float rotatingAngle = Vector3.Angle(BA, BC);
    188.  
    189.                     Debug.Log("rotatingAngle " + rotatingAngle);
    190.  
    191.                     TogglePlaneDetection();
    192.                     changeBuildingPosition(BuildingPosition, child);
    193.  
    194.  
    195.                     if ((Cube1.x > Cube2.x) || (Cube2.z > Cube1.z))
    196.                     {
    197.                         //StartCoroutine(Waiter(-rotatingAngle, positions[0]));
    198.                         changeRotation(-rotatingAngle, positions[0]);
    199.                     }
    200.                     else
    201.                     {
    202.                         //StartCoroutine(Waiter(rotatingAngle, positions[0]));
    203.                         changeRotation(rotatingAngle, positions[0]);
    204.                     }
    205.             }
    206.             }
    207.         }
    208.     }
    209.  
    210.  
    211.     private void TogglePlaneDetection()
    212.     {
    213.         arPlaneManager.enabled = !arPlaneManager.enabled;
    214.  
    215.         foreach (ARPlane plane in arPlaneManager.trackables)
    216.         {
    217.             plane.gameObject.SetActive(arPlaneManager.enabled);
    218.         }
    219.  
    220.         toggleButton.GetComponentInChildren<Text>().text = arPlaneManager.enabled ? "Disable Plane Detection" : "Enable Plane Detection";
    221.     }
    222.  
    223.  
    224.     private void changeBuildingPosition(Vector3 ItemPosition, Transform child)
    225.     {
    226.         foreach (Transform children in child)
    227.         {
    228.             children.position = ItemPosition;
    229.         }
    230.  
    231.     }
    232.  
    233.     private void changeRotation(float rotationAngle, Vector3 position)
    234.     {
    235.         //child.RotateAround(position, new Vector3(0, 1, 0), rotationAngle);
    236.  
    237.         foreach (Transform children in child)
    238.         {
    239.             children.RotateAround(position, new Vector3(0, 1, 0), rotationAngle);
    240.         }
    241.  
    242.     }
    243.  
    244. }
    245.  
     
  2. dirnanm

    dirnanm

    Joined:
    Feb 9, 2022
    Posts:
    3
    Hello have you managed to do it?