Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Using Raycast with Prefabs Instantiated using Tracked Image Manager

Discussion in 'AR' started by kyle119365, May 22, 2023.

  1. kyle119365

    kyle119365

    Joined:
    Mar 23, 2023
    Posts:
    8
    Hi There,

    I have been trying for a while and it seems that the prefab spawned by the tracked image manager cant be hit by the physics raycast. If I drag the same prefab into the scene the raycast hit's it fine, does anyone know why this is occurring or how to fix it?
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    How are you verifying whether the prefab is hit?.

    Does the tracked image manager make the instantiated prefab a child of another gameobject? If so then you're probably getting the parent's gameobject info and not the actual prefab.

    If the above is true then use something like: Debug.Log(hit.collider.transform.name); to see the actual hit object's name and not the parent's.
     
  3. kyle119365

    kyle119365

    Joined:
    Mar 23, 2023
    Posts:
    8
    Thank you for your reply I believe that is what I am using, this is how I instantiate the prefab.
    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.XR.ARFoundation;
    5. using UnityEngine.XR.ARSubsystems;
    6. [RequireComponent(typeof(ARTrackedImageManager))]
    7. public class PlaceTrackedImages : MonoBehaviour
    8. {
    9.     private ARTrackedImageManager _trackedImagesManager;
    10.     public GameObject[] ArPrefabs;
    11.     private readonly Dictionary<string, GameObject> _instantiatedPrefabs = new Dictionary<string, GameObject>();
    12.     void Awake()
    13.     {
    14.         _trackedImagesManager = GetComponent<ARTrackedImageManager>();
    15.     }
    16.     private void OnEnable()
    17.     {
    18.         _trackedImagesManager.trackedImagesChanged += OnTrackedImagesChanged;
    19.     }
    20.     private void OnDisable()
    21.     {
    22.         _trackedImagesManager.trackedImagesChanged -= OnTrackedImagesChanged;
    23.     }
    24.     private void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs)
    25.     {
    26.         foreach (var trackedImage in eventArgs.added)
    27.         {
    28.             var imageName = trackedImage.referenceImage.name;
    29.             foreach (var curPrefab in ArPrefabs)
    30.             {
    31.                 if (string.Compare(curPrefab.name, imageName, StringComparison.OrdinalIgnoreCase) == 0 && !_instantiatedPrefabs.ContainsKey(imageName))
    32.                 {
    33.                     var newPrefab = Instantiate(curPrefab, trackedImage.transform);
    34.                     _instantiatedPrefabs[imageName] = newPrefab;
    35.                 }
    36.             }
    37.         }
    38.         foreach (var trackedImage in eventArgs.updated)
    39.         {
    40.             _instantiatedPrefabs[trackedImage.referenceImage.name].SetActive(trackedImage.trackingState == TrackingState.Tracking);
    41.         }
    42.         foreach (var trackedImage in eventArgs.removed)
    43.         {
    44.             Destroy(_instantiatedPrefabs[trackedImage.referenceImage.name]);
    45.             _instantiatedPrefabs.Remove(trackedImage.referenceImage.name);
    46.         }
    47.     }
    48. }
    And this is the code I used for the raycast on touch, it prints the name of what was hit to the TMP text element :)

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using TMPro;
    5.  
    6. public class PlacementWithManySelectionController : MonoBehaviour
    7. {
    8.     private Camera arCamera;
    9.     private Vector2 touchPosition = default;
    10.     public Button swapObject;
    11.     public TextMeshProUGUI objectName;
    12.     public int object1TypeTracker = 0;
    13.     public int object2TypeTracker = 0;
    14.     public string collidedObject;
    15.  
    16.     void Awake()
    17.     {
    18.         objectName.text = "";
    19.         swapObject.onClick.AddListener(SwapObjectTracker);
    20.     }
    21.  
    22.     void SwapObjectTracker()
    23.     {
    24.         if (collidedObject.Contains("RoboSparky"))
    25.         {
    26.             if (object1TypeTracker == 0)
    27.             {
    28.                 object1TypeTracker = 1;
    29.                 objectName.text = "Robo Sparky";
    30.             }
    31.  
    32.             if (object1TypeTracker == 1)
    33.             {
    34.                 object1TypeTracker = 0;
    35.                 objectName.text = "Robo Sparky";
    36.             }
    37.         }
    38.  
    39.         if (collidedObject.Contains("ASUPitchfork"))
    40.         {
    41.             if (object2TypeTracker == 0)
    42.             {
    43.                 object2TypeTracker = 1;
    44.                 objectName.text = "ASU Pitchfork";
    45.             }
    46.  
    47.             if (object2TypeTracker == 1)
    48.             {
    49.                 object2TypeTracker = 0;
    50.                 objectName.text = "ASU Logo";
    51.             }
    52.         }
    53.     }
    54.  
    55.     void Update()
    56.     {
    57.         if (Input.touchCount > 0)
    58.         {
    59.             Touch touch = Input.GetTouch(0);
    60.  
    61.             touchPosition = touch.position;
    62.  
    63.             if(touch.phase == TouchPhase.Began)
    64.             {
    65.                 Ray ray = arCamera.ScreenPointToRay(touch.position);
    66.                 RaycastHit hitObject;
    67.                 if (Physics.Raycast(ray, out hitObject))
    68.                 {
    69.                     collidedObject = hitObject.collider.gameObject.name;
    70.                     objectName.text = collidedObject;
    71.                 }
    72.             }
    73.         }
    74.     }
    75. }
     
  4. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    I'm surprised your 2nd script works. Why isn't your arCamera public for setting in the inspector?.

    Or can you try tagging your Camera 'MainCamera' and then use: Camera.main.ScreenPointToRay(touch.position)?.
     
    kyle119365 likes this.
  5. kyle119365

    kyle119365

    Joined:
    Mar 23, 2023
    Posts:
    8
    Yea now that you mention it I am not sure, let me try making it public then attaching the ar camera to it, I will let you know if that works :)
     
  6. kyle119365

    kyle119365

    Joined:
    Mar 23, 2023
    Posts:
    8
    Thank you for your help, adding the camera fixed part of it, then removing the AR plane manager fixed the other issue, I guess it was creating planes over my objects and I was selecting them instead of my objects :)
     
    andyb-unity likes this.
  7. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    Awesome! :)