Search Unity

How to properly toggle Face Tracking?

Discussion in 'AR' started by Christin2015, Oct 15, 2019.

  1. Christin2015

    Christin2015

    Joined:
    Dec 1, 2015
    Posts:
    48
    Hi,

    I want to use Face Tracking , then on button click dis-/ enable the face tracking. When the face tracking is disabled, I want to attach a 3d model to the AR camera (rear cam) and vice versa. I am using the following code to toggle the face tracking, is it the correct way?

    Code (CSharp):
    1. void ToggleTracking()
    2.     {
    3.         if (_arFaceManager.enabled) // attach model to the rear AR camera
    4.         {
    5.             _arFaceManager.enabled = false;
    6.             _trackedFace.GetComponent<MeshRenderer>().enabled = false;
    7.             _trackedFace.GetComponent<ARFaceMeshVisualizer>().enabled = false;
    8.             _trackedFace.GetComponent<ARFace>().enabled = false;
    9.  
    10.             _currentProduct.transform.parent = Camera.main.transform;
    11.             _trackedFace.gameObject.SetActive(false);
    12.         }
    13.         else  // attach model to the tracked face
    14.         {
    15.             // first enable the ArFaceManager
    16.             _arFaceManager.enabled = true;
    17.             _trackedFace.gameObject.SetActive(true);
    18.             _trackedFace.GetComponent<ARFace>().enabled = true;
    19.             _trackedFace.GetComponent<ARFaceMeshVisualizer>().enabled = true;
    20.             _trackedFace.GetComponent<MeshRenderer>().enabled = true;
    21.  
    22.             _currentProduct.transform.parent = _trackedFace.transform;
    23.         }
    24.     }
    25.  
    I am using ARFoundation 3.0.