Hey everyone, for an Android application I want to implement AR Image Detection and if the image is detected I need STABLE positioning and rotation. Right now its super buggy.
I have on my prefabs anchors and in my scene the anchor manager. When the image gets detected the prefab is there but everytime with a different rotation.
Here is my code:
private void Awake()
{
_arTrackedImageManager = GetComponent<ARTrackedImageManager>();
foreach (GameObject arObject in arObjectsToPlace)
{
GameObject newARObject = Instantiate(arObject, Vector3.zero, Quaternion.identity);
newARObject.name = arObject.name;
newARObject.SetActive(false);
spawnedARObjects.Add(newARObject.name, newARObject);
}
}
private void Start()
{
animated = false;
UpdateFlowStateBtn.SetActive(false);
}
public void OnEnable()
{
//subscribe to event of image detected
_arTrackedImageManager.trackedImagesChanged += OnImageChanged;
}
public void OnDisable()
{
_arTrackedImageManager.trackedImagesChanged -= OnImageChanged;
}
public void OnImageChanged(ARTrackedImagesChangedEventArgs eventArgs)
{
foreach (ARTrackedImage trackedImage in eventArgs.added)
{
UpdateARImage(trackedImage);
}
foreach (ARTrackedImage trackedImage in eventArgs.updated)
{
UpdateARImage(trackedImage);
}
foreach (ARTrackedImage trackedImage in eventArgs.removed)
{
spawnedARObjects[trackedImage.name].SetActive(false);
}
}
private void UpdateARImage(ARTrackedImage trackedImage)
{
AssignGameObject(trackedImage.referenceImage.name, trackedImage.transform.localPosition, trackedImage.transform.rotation);
}
private void AssignGameObject(string name, Vector3 newPosition, Quaternion rotationImage)
{
if (arObjectsToPlace != null)
{
spawnedARObjects[name].transform.position = newPosition;
spawnedARObjects[name].transform.rotation = rotationImage;
if (!spawnedARObjects[name].activeSelf) spawnedARObjects[name].SetActive(true);
if (!animated)
{
StartCoroutine(AnimationARObject(spawnedARObjects[name]));
}
}
}
private IEnumerator AnimationARObject(GameObject AROb)
{
yield return new WaitForSeconds(0.1f);
if (AROb.name == "ARFahndungsFoto")
{
AROb.LeanScale(scaleFactor * .8f, 2f).setEaseOutCubic().setOnComplete(ShowBtnUpdateFlow);
//AROb.transform.localRotation = Quaternion.Euler(0f, 180f, 0f);
}
else if (AROb.name == "ARZelt")
{
AROb.LeanScale(Vector3.one * .8f, 2f).setEaseOutCubic().setOnComplete(ShowARZeltUI);
}
else if (AROb.name == "ARTonband")
{
AROb.LeanScale(scaleFactor, 2f).setEaseOutCubic().setOnComplete(ShowARTonBand);
}
animated = true;
}
Thanks in advance. Appreciate it