Hello
So I have tried implementing the code previously posted in this thread and it doesn't seem to work and it actually seems to break the DefaultTrackableEventHandler. My models will sort of stick in one corner of the screen even when the tracker is not visible. But that is only one part of the problem, which is no sound playing when the trackers are visible. I've seen some videos of people refering to a "customTrackableEventHandler" component but I can't find it in the 5-0-6 Vuforia package. There also doesn't seem to be the ITrackableEventHandler component in the package.
I have also tried doing what was mentionned in this tutorial, but with no luck : https://developer.vuforia.com/forum/faq/unity-how-can-i-play-audio-when-targets-get-detected
I'm using Unity 5.2.1.
Anyways, here is the code on the DefaultTrackableEvenHandler in the ImageTarget prefab:
/*==============================================================================
Copyright (c) 2010-2014 Qualcomm Connected Experiences, Inc.
All Rights Reserved.
Confidential and Proprietary - Qualcomm Connected Experiences, Inc.
==============================================================================*/
using UnityEngine;
namespace Vuforia
{
/// <summary>
/// A custom handler that implements the ITrackableEventHandler interface.
/// </summary>
public class DefaultTrackableEventHandler : MonoBehaviour,
ITrackableEventHandler
{
#region PRIVATE_MEMBER_VARIABLES
private TrackableBehaviour mTrackableBehaviour;
#endregion // PRIVATE_MEMBER_VARIABLES
#region UNTIY_MONOBEHAVIOUR_METHODS
void Start()
{
mTrackableBehaviour = GetComponent<TrackableBehaviour>();
if (mTrackableBehaviour)
{
mTrackableBehaviour.RegisterTrackableEventHandler(this);
}
}
#endregion // UNTIY_MONOBEHAVIOUR_METHODS
#region PUBLIC_METHODS
/// <summary>
/// Implementation of the ITrackableEventHandler function called when the
/// tracking state changes.
/// </summary>
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();
}
}
#endregion // PUBLIC_METHODS
#region PRIVATE_METHODS
private void OnTrackingFound()
{
Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
// *** Additional Audio code
foreach (Transform child in transform)
{
child.GetComponent<AudioSource>().Play();
}
// Enable rendering:
foreach (Renderer component in rendererComponents)
{
component.enabled = true;
}
// Enable colliders:
foreach (Collider component in colliderComponents)
{
component.enabled = true;
}
Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " found");
}
private void OnTrackingLost()
{
Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
// *** Additional Audio code
foreach (Transform child in transform)
{
child.GetComponent<AudioSource>().Stop();
}
// Disable rendering:
foreach (Renderer component in rendererComponents)
{
component.enabled = false;
}
// Disable colliders:
foreach (Collider component in colliderComponents)
{
component.enabled = false;
}
Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
}
#endregion // PRIVATE_METHODS
}
}
Hi
I had that problem and i solved it . you must first scale your object in your scene as the real size in world.
For example if you have a bottle on a table your distance from the target must be maximum 2 meter .
Audio source also must be attached to the target object too and
the camera position must be far enough that the sound not be able to heard when
you are ready to build the project.
Do not forget to check on "play on awake " in Audio source component.
Good luck.