Search Unity

Question Placing and Manipulating Objects in AR Help

Discussion in 'Getting Started' started by UMB_AR_Design, Jan 3, 2023.

  1. UMB_AR_Design

    UMB_AR_Design

    Joined:
    Jan 3, 2023
    Posts:
    1
    I am trying to follow the Unity Learn project titled Placing and Manipulating Objects in AR. I get to step
    4.12- In SpawnableManager Script on your SpawnManager object, drag the AR Session Origin object from the hierarchy into the Raycast Manager variable in the inspector.

    My SpawnManager does not look like the picture shown and I cannot drag any assets to the be attached to the spawnable manager script. I have included the code they ask us to write and the image of my Unity Hub Project compared to their scripts and their Unity Hub references.

    Code (CSharp):
    1. //My CODE
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using UnityEngine.XR.ARFoundation;
    7.  
    8. public class SpawnableManager : MonoBehaviour
    9. {
    10.     [SerializeField]
    11.     ARRaycastManager m_RaycastManager;
    12.     List<ARRaycastHit> m_Hits = new List<ARRaycastHit>();
    13.     [SerializeField]
    14.     GameObject spawnablePrefab;
    15.  
    16.     Camera arCam;
    17.     GameObject spawnedObject;
    18.  
    19.     public ARRaycastManager RaycastManager { get => m_RaycastManager; set => m_RaycastManager = value; }
    20.  
    21.     // Start is called before the first frame update
    22.     void Start()
    23.     {
    24.         spawnedObject = null;
    25.         arCam = GameObject.Find("AR Camera").GetComponent<Camera>();
    26.  
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update()
    31.     {
    32.         if (Input.touchCount == 0)
    33.             return;
    34.  
    35.         RaycastHit hit;
    36.         Ray ray = arCam.ScreenPointToRay(Input.GetTouch(0).position);
    37.  
    38.         if (RaycastManager.Raycast(Input.GetTouch(0).position, m_Hits))
    39.         {
    40.             if(Input.GetTouch(0).phase == TouchPhase.Began && spawnedObject == null)
    41.             {
    42.                 if (Physics.Raycast(ray, out hit))
    43.                 {
    44.                     if (hit.collider.gameObject.tag == "Spawnable")
    45.                     {
    46.                         spawnedObject = hit.collider.gameObject;
    47.                     }
    48.                     else
    49.                     {
    50.                         SpawnPrefab(m_Hits[0].pose.position);
    51.                     }
    52.                 }
    53.  
    54.             }
    55.             else if(Input.GetTouch(0).phase == TouchPhase.Moved && spawnedObject != null)
    56.             {
    57.                 spawnedObject.transform.position = m_Hits[0].pose.position;
    58.             }
    59.             if(Input.GetTouch(0).phase == TouchPhase.Ended)
    60.             {
    61.                 spawnedObject = null;
    62.  
    63.             }
    64.  
    65.         }
    66.     }
    67.     private void SpawnPrefabe(Vector3 spawnPosition)
    68.     {
    69.         spawnedObject = Instantiate(spawnablePrefab, spawnPosition, Quaternion.identity);
    70.     }
    71. }
    72. //MY CODE
    EXAMPLE CODE
    upload_2023-1-3_15-19-6.png






    My HUB
    upload_2023-1-3_15-18-26.png


    REFERENCE HUB
    upload_2023-1-3_15-19-47.png