Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How can I get the entire world to be tracked to an image in Untiy?

Discussion in 'AR' started by Observer3110, Sep 18, 2023.

  1. Observer3110

    Observer3110

    Joined:
    Feb 21, 2021
    Posts:
    3
    I'm trying to create a AR Image tracking app and I ran into some issues, after narrowing it down I understood that the issue is due to the coordinates of the Tracked Image Prefab.

    I put the current coordinates of the Tracked Image Prefab on a text on the screen. If the coordinates of the Prefab is always stuck at 0 0 0 all my issues will be solved immediately, but that's not the case. The coordinates of the Prefab are different each time I run the app, not to mention it keeps changing as i move the Camera around. Is there a way to lock the (0,0,0) of the world to the IRL Image I have?
     
    harrylingwal likes this.
  2. harrylingwal

    harrylingwal

    Joined:
    Jun 20, 2023
    Posts:
    1
    I am trying to get the world coordinate of the prefab instantiated through ARTrackedImageManager but the result is always (0,0,0) can you share your peice of code ? i think i might help me with the problem....

    Code (CSharp):
    1.  
    2.     private void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs)
    3.     {
    4.         foreach (var trackedImage in eventArgs.added)
    5.         {
    6.             // Instantiate the prefab at the image's world position
    7.             GameObject instantiatedPrefab = Instantiate(prefabToInstantiate, trackedImage.transform.position, trackedImage.transform.rotation);
    8.             world = trackedImage.transform.position;
    9.             pos.text = "Position : " + world.ToString();
    10.         }
    11.     }
    12. }
    this code is generating (0,0,0)
     
  3. Observer3110

    Observer3110

    Joined:
    Feb 21, 2021
    Posts:
    3
    Thank you for the reply,
    That's actually another thing that I'm curious at, I'm searching for the piece of code that actually instantiates the prefab, but I can't seem to find it at all, and I don't have any OnTrackedImagesChanged method at all.
    I'm not an expert on the matter, so all I've done is add a script that keeps updating the text on screen with the coordinates of a game object. From that I got that the ARSessionOrigin is always at (0,0,0), ARSession and the Prefab moves around a lot based on the movement of the camera.
     
    harrylingwal likes this.
  4. xiennastudio

    xiennastudio

    Joined:
    Aug 2, 2023
    Posts:
    19
    The
    ARTrackedImageManager.cs
    is the script that will instantiate the prefab. I don't think we will be able to see the exact line.

    The OnTrackedImagesChanged is most probably from this script https://github.com/Unity-Technologi...BasicImageTracking/TrackedImageInfoManager.cs

    If you want to get the Tracked image position as (0,0,0), is getting the prefab inside the Tracked image's local position help with it? Or it need to be the Tracked image itself?
     
  5. andyb-unity

    andyb-unity

    Unity Technologies

    Joined:
    Feb 10, 2022
    Posts:
    956
    Generally you would use Transform.SetParent to set your content to be a child of the ARTrackedImage GameObject, then set your content's local position and rotation to 0.
     
  6. Observer3110

    Observer3110

    Joined:
    Feb 21, 2021
    Posts:
    3
    Thank you for your responses
    What I'm trying to achieve is a simple AR Multiplayer game. The current setup is a plane with 2 spawn points on it. The issue is that the coordinates aren't matching up on the 2 devices. On the host device, I'm getting the coordinates of the spawn points. When the client device connects it takes the coordinates from the host and uses them, but the plane is on very different coordinates than on the host device and thus the spawned objects are very far away. If I can get the coordinates of the plane to be set on 0,0,0 and stick that to the Image, it would work out. The other option being using coordinates relative to the tracked image prefab instead of world coordinates, which I have not yet figured out how to achieve that yet. Any further guidance regarding the matter is very much appreciated.
     
  7. andyb-unity

    andyb-unity

    Unity Technologies

    Joined:
    Feb 10, 2022
    Posts:
    956
    As you say, you could achieve this by using coordinates relative to the tracked image prefab. (Parent your content to the tracked image prefab, then send local transform data over the network.)

    Alternatively you could consider Niantic ARDK which has a multiplayer solution built in.