I am using ARKit's image tracking to place an object at a certain position. If the image is lost, I want the object to stay in the same position as it got tracked initially. But unfortunately the extended tracking is no success. When the image is lost, the object takes in a different position farther away from where it was initially tracked. What should I do in this script?
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
public class ARImageAnchor : MonoBehaviour
{
public string ReferenceImageName;
private ARTrackedImageManager _TrackedImageManager;
public Text text_;
public ARTrackedImageManager arTracked;
private void Awake()
{
_TrackedImageManager = FindObjectOfType<ARTrackedImageManager>();
}
private void OnEnable()
{
if (_TrackedImageManager != null)
{
_TrackedImageManager.trackedImagesChanged += OnTrackedImagesChanged;
}
}
private void OnDisable()
{
if (_TrackedImageManager != null)
{
_TrackedImageManager.trackedImagesChanged -= OnTrackedImagesChanged;
}
}
private void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs e)
{
foreach (var trackedImage in e.added)
{
Debug.Log($"Tracked image detected: {trackedImage.referenceImage.name} with size: {trackedImage.size}");
if(trackedImage.referenceImage.name == this.ReferenceImageName)
{
this.gameObject.GetComponent<MeshRenderer>().enabled = true;
}
}
UpdateTrackedImages(e.added);
UpdateTrackedImages(e.updated);
}
private void UpdateTrackedImages(IEnumerable<ARTrackedImage> trackedImages)
{
// If the same image (ReferenceImageName)
var trackedImage =
trackedImages.FirstOrDefault(x => x.referenceImage.name == ReferenceImageName);
if (trackedImage == null)
{
return;
}
if (trackedImage.trackingState != TrackingState.None)
{
var trackedImageTransform = trackedImage.transform;
transform.SetPositionAndRotation(new Vector3(trackedImageTransform.position.x + 0.5f, trackedImageTransform.position.y, trackedImageTransform.position.z), Quaternion.Euler(0, 0, 0));
}
}
}
question from:
https://stackoverflow.com/questions/65842563/extended-image-tracking-arkit 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…