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

Question Access GameObjects created by AR Face Manager

Discussion in 'AR' started by liu550, Nov 7, 2021.

  1. liu550

    liu550

    Joined:
    Apr 2, 2021
    Posts:
    2
    I am using AR Foundation to build an application that has a world-facing AR Camera and a AR Face Manager. The idea is to use facial features tracking to animate a cartoon face and display it in the physical environment, like the example given here (https://developer.apple.com/documen...mbining_user_face-tracking_and_world_tracking). I added the AR Face component to my face prefab and assigned the prefab to the corresponding field in AR Face Manager, and this facial tracking part worked fine.

    However, I am not sure about how to access the GameObject created by AR Face Manager, namely the animated cartoon face, such that it can be placed at arbitrary physical positions at runtime. I have not found any up-to-date documentation regarding how to implement this feature and would appreciate your help.
     
  2. todds_unity

    todds_unity

    Joined:
    Aug 1, 2018
    Posts:
    324
    KyryloKuzyk likes this.
  3. swishchee

    swishchee

    Joined:
    Jul 23, 2020
    Posts:
    63
    But, how do you Access GameObjects created by AR Face Manager?

    I understand how to reference the prefab. However, I'm interested in accessing the actual face that is created.
    I've explored all of the methods for ARFaceManager, and the closest one I could find was _faceManager.TryGetFace(TrackableId faceId), but this does not seem relevant to what I'm looking for, as I wouldn't know ID.

    [EDIT] There is no way to do so. You must populate everything into whatever prefab is spawned, and use GameObject.FindWithTag (or some other find method) to gain access to it. I'm kind of surprised that this is the official recommendation.
     
    Last edited: Jul 26, 2023
  4. andyb-unity

    andyb-unity

    Unity Technologies

    Joined:
    Feb 10, 2022
    Posts:
    1,025
    swishchee likes this.
  5. swishchee

    swishchee

    Joined:
    Jul 23, 2020
    Posts:
    63
    Thanks for the reply. I indeed get reference to objects once the facesChanged event is invoked. However, when I tried directly accessing the transform of the spawned gameobject with the ARFace component on it, my objects would seemingly spawn in random locations, while rotating properly with my head. I ran this test with small cubes, and I'd end up with these cubes spawning ~0.6 - 1 meter in front of my face, sometimes to the left or right. It was very unpredictable. This is what I used to access that transform:

    Code (CSharp):
    1. foreach(ARFace face in _faceManager.trackables)
    2.             {
    3.                 GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    4.                 cube.transform.parent = face.transform;
    5.                 cube.transform.localScale = new Vector3(0.01f,0.01f,0.01f);
    6.             }
     
  6. WyattBUnity

    WyattBUnity

    Unity Technologies

    Joined:
    Mar 27, 2023
    Posts:
    43
    It looks to me like you are not repositioning the cubes after you create them, rather you are parenting them (which would make them move with your face properly like you mentioned) but not actually having them properly positioned at your face.
    One possible solution is to use Transform.SetParent and pass false to the boolean , which will relocate the object you've created. Alternatively, you can set the local position to some value that is to your liking.
     
    andyb-unity and swishchee like this.
  7. swishchee

    swishchee

    Joined:
    Jul 23, 2020
    Posts:
    63
    Oh, very interesting! That makes a lot of sense, actually. For some reason, I was assuming that the cubes would just spawn at the origin, but I hadn't considered that setting the parent might preserve the world coordinates. I'll note this information for when I'm back on that project.