Hello. I was trying to find a way to make my AR objects disable when they are not in view (target lost).
The problem I'm having is that I have several animals on cards and have a script that when they are clicked they play an action. When the card is by itself, I can click on it and it plays the correct audio. However, when it's not in view anymore (target lost) and I click - it plays ALL the audio of the cards. Not sure why but is there a way to disable an object when target lost so it ensures that it doesn't conflict with other things? Would really appreciate some help with this.
That's correct,
one way to do that is to have a script like the below attached to each Image Tracker object
using UnityEngine;
using Vuforia;
//Attach to the image tracker
public class ChildObjectsActivator : MonoBehaviour, ITrackableEventHandler
{
private TrackableBehaviour trackableBehaviour;
void Start()
{
trackableBehaviour = GetComponent<TrackableBehaviour>();
if (trackableBehaviour)
trackableBehaviour.RegisterTrackableEventHandler(this);
}
public void OnTrackableStateChanged(
TrackableBehaviour.Status previousStatus,
TrackableBehaviour.Status newStatus)
{
if (newStatus == TrackableBehaviour.Status.DETECTED ||
newStatus == TrackableBehaviour.Status.TRACKED ||
newStatus == TrackableBehaviour.Status.EXTENDED_TRACKED)
OnTrackingFound();
else
onTrackingLost();
}
private void OnTrackingFound()
{
if (transform.childCount > 0)
SetChildrenActive(true);
}
private void onTrackingLost()
{
if (transform.childCount > 0)
SetChildrenActive(false);
}
private void SetChildrenActive(bool activeState)
{
for (int i = 0; i <= transform.childCount; i++)
transform.GetChild(i++).gameObject.SetActive(activeState);
}
}