Search Unity

Bug Trying to place cube on ARFace (AR Foundation)

Discussion in 'AR' started by aditdesai, May 2, 2023.

  1. aditdesai

    aditdesai

    Joined:
    Sep 11, 2020
    Posts:
    2
    I'm trying to make an AR Flappy Bird game where the flappy bird is attached to the nose tip of the face tracked by ARFoundation's Face Tracker. For the prototype, I'm using a simple 3D cube and trying to set it's position to the nose tip of the AR Face when a face is detected. When I build it and test it on my android phone, the cube is visible initially when I haven't pointed the camera to my face (as it should). As soon as I bring my face in front of it, it detects my face correctly (face mask renders correctly) but the cube disappears. Either the cube is being positioned somewhere outside the view of the camera or it's too small to be visible or there is some other error. It might have something to do with localPosition and position since the cube is a child of AR Session Origin. This is the script that I have attached to AR Session Origin to set the cube's position:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.XR.ARFoundation;
    3. using UnityEngine.XR.ARSubsystems;
    4. public class BirdController : MonoBehaviour
    5. {
    6.     [SerializeField] ARFaceManager faceManager;
    7.     [SerializeField] Transform flappyBird;
    8.     void Start()
    9.     {
    10.         faceManager = GetComponent<ARFaceManager>();
    11.         faceManager.facesChanged += OnFacesChanged;
    12.     }
    13.     void OnFacesChanged(ARFacesChangedEventArgs args)
    14.     {
    15.         foreach (ARFace face in args.updated)
    16.         {
    17.             if (face.trackingState == TrackingState.Tracking)
    18.             {
    19.                 Vector3 noseTipPosition = face.vertices[9]; // the index for the nose tip vertex may vary depending on the device and face mesh configuration
    20.                 flappyBird.localPosition = noseTipPosition;
    21.             }
    22.         }
    23.     }
    24. }
    In case you're wondering, yes, I have referenced faceManager and flappyBird correctly. I'm unsure about "face.vertices[9]" (it was something chatgpt suggested).
    I also tried using this instead of the entire OnFacesChanged logic:


    Code (CSharp):
    1. void Update()
    2.     {
    3.         foreach (ARFace face in faceManager.trackables)
    4.         {
    5.             Vector3 noseTipPos = face.vertices[9] + new Vector3(0, 0, 1f); //offset in case its being rendered at the cam's position
    6.             flappyBird.position = noseTipPos;
    7.         }
    8.     }
    This is the hierarchy and inspector window for AR Session Origin:

    upload_2023-5-3_0-48-14.png

    What am I doing wrong? It must be the code but I can't seem to figure out why. I've tried changing position to localPosition, face.vertices[9] to face.leftEye.position (in case that works) but nothing seems to work. Is there a completely different approach I should be taking to making this game?
     
  2. aditdesai

    aditdesai

    Joined:
    Sep 11, 2020
    Posts:
    2