Search code examples
c#unity-game-enginevuforia

Fixed Position for 3D Model on ImageTarget with Vuforia


I am developing an AR application, with Vuforia ImageTarget I added a QR code to be scanned. When scan the qr code I want my 3d model appear with fixed point and scale on the screen and do not track ImageTarget anymore.

Default Observer Event Handler script at Vuforia Package has 3 options which Tracked, Tracked or Extended Tracked and Tracked,Extenden Tracked or Limited but non of these options are not satisfy what I want. I just want to scan QR code once the model should appears on screen with fixed point and scale then 3d model should not track ImageTarget anymore. Also I read Vuforia Images&Objects Library but can not find a useful tip.


Solution

  • First of all thanks to all who contribute to solve this problem. With help of @charanvendra I revise the code and finally solve the problem. For those who is faced with similar problems like I do the code :

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using Vuforia;
    
    public class QRTrackerHandler : MonoBehaviour
    {
        private ObserverBehaviour mObserverBehaviour;
        private bool isModelDisplayed = false;
    
        public GameObject model;  // Assign 3D model in the inspector
    
        private GameObject modelContainer; // Create a container which positioned according to camera
    
        void Start()
        {
            mObserverBehaviour = GetComponent<ObserverBehaviour>();
            if (mObserverBehaviour)
            {
                mObserverBehaviour.OnTargetStatusChanged += OnTargetStatusChanged;
            }
    
            if (model != null)
            {
                model.SetActive(false);  // Initially hide the model
                CreateModelContainer();
            }
        }
    
        private void OnDestroy()
        {
            if (mObserverBehaviour)
            {
                mObserverBehaviour.OnTargetStatusChanged -= OnTargetStatusChanged;
            }
        }
    
        private void OnTargetStatusChanged(ObserverBehaviour behaviour, TargetStatus targetStatus)
        {
            if (!isModelDisplayed && (targetStatus.Status == Status.TRACKED ||
                                      targetStatus.Status == Status.EXTENDED_TRACKED))
            {
                DisplayModel();
                isModelDisplayed = true;
                DisableTracking();
            }
        }
    
        private void CreateModelContainer()
        {
            modelContainer = new GameObject("ModelContainer");
            modelContainer.transform.SetParent(Camera.main.transform);
            modelContainer.transform.localPosition = new Vector3(0, 0, 275);  // Adjust the Z value as needed
            modelContainer.transform.localRotation = Quaternion.Euler(-90, 0, 0); // RotateModel to show as needed
            modelContainer.transform.localScale = Vector3.one;
    
            model.transform.SetParent(modelContainer.transform);
            model.transform.localPosition = Vector3.zero;
            model.transform.localRotation = Quaternion.identity;
            model.transform.localScale = Vector3.one;
        }
    
        private void DisplayModel()
        {
            if (model != null)
            {
                model.SetActive(true);  // Show the model
            }
        }
    
        private void DisableTracking()
        {
            if (mObserverBehaviour)
            {
                mObserverBehaviour.OnTargetStatusChanged -= OnTargetStatusChanged;
            }
        }
    }
    

    Do not need to use System.Collections;