Reputation: 123
Currently im doing a AR audio book which is when scan multiple image target the sound will play for each target. Im trying to do follow the tutorial given by Unity and vuforia but still no working. can u help me out.
This scene contains bird and tiger. When image bird is scan the bird sound will play also the tiger.
This is my code:
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Vuforia;
public class ImageTargetPlayAudio : MonoBehaviour,
ITrackableEventHandler
{
private TrackableBehaviour mTrackableBehaviour;
void Start()
{
mTrackableBehaviour = GetComponent<TrackableBehaviour>();
if (mTrackableBehaviour)
{
mTrackableBehaviour.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)
{
// Play audio when target is found
GetComponent<AudioSource>().Play();
}
else
{
// Stop audio when target is lost
GetComponent<AudioSource>().Stop();
}
}
}
And image attached is my hierarchy.
I adding this code into each image target.
Correct me if im wrong.
Upvotes: 1
Views: 1331
Reputation: 678
If you are new to Vuforia you will be able to handle it easier with GetComponent
on the DefaultTrackableEventHandler
, that script contains the OnTrackingLost
and OnTrackingFound
events already working.
Just add a GetComponent
for your custom script and if not null
, play the sound on that script.
Upvotes: 1
Reputation: 27021
Docs for RegisterTrackableEventHandler
This method registers a new Tracker event handler at the Tracker. These handlers are called as soon as ALL Trackables have been updated in this frame.
So you use the wrong method, check the document, maybe OnTrackerUpdate
is appropriate.
Upvotes: 1