Log in or register to post comments

ASK Unity AR Sound

December 17, 2013 - 9:10am #1

Excuse me, anyone know how to set the sound be play if the object appears in Unity3D ? 

If my object appear, my sound will play,
and if my object not appear, that sound will be pause / stop ?

Please reply, thanks before :D

ASK Unity AR Sound

December 18, 2013 - 8:53am #6

Make sure you dont have loop checked on the audio...and just check if the audio is playing.. If yes stop it and resume.. If you loose tracking stop the audio as well
Greets

ASK Unity AR Sound

December 18, 2013 - 6:45am #5

paatz04 wrote:

What do you mean with when the object dissapears? Just hold a reference variable, to the object.. Or set the variable when u create the instance

For example:
When the application runs, audio source muted until the marker is detected, and when marker is lost, audio will be pause/stop until the marker is detected again.

The following code works when the marker detected, my audio will play and the audio still play even though the marker isnt detected/lose.
Resulting, when marker detected again, the sound be collision (2 same audio playing).

And I want, when the marker is not detected, the audio will pause / stop, and the audio will resume / play when the marker is detected again, so there is no collision sound. 

Sorry for my bad english, i hope you understand what i mean XD
I just dont know the command code to make audio will pause/stop when the marker (object) tracking is lost.

ASK Unity AR Sound

December 18, 2013 - 4:19am #4

What do you mean with when the object dissapears? Just hold a reference variable, to the object.. Or set the variable when u create the instance

ASK Unity AR Sound

December 18, 2013 - 3:35am #3

paatz04 wrote:

Yea that shoud be pretty easy,

 

just listen(Dellegate or whatever) to the 2 Methods TrackingLost and TrackingFound you can find in the DefaultTrackableBehaviour Script, and when one of those is called, handle it and play the sound...

Greetings 

Paatz :)

Thanks for the reply, I managed to play sound, but I do not know how to pause / stop when the object is lost ..

This is my DefaultTrackableEventHandler Script :


public class DefaultTrackableEventHandler : MonoBehaviour,
                                            ITrackableEventHandler
{

    private TrackableBehaviour mTrackableBehaviour;
    public AudioClip test; //im add this script
    
    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)
        {
            OnTrackingFound();
        }
        else
        {
            OnTrackingLost();
        }
    }

    private void OnTrackingFound()
    {
        Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
        Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);

        foreach (Renderer component in rendererComponents)
        {
        audio.PlayOneShot(test); //im add this script
	component.enabled = true;
        }

        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);

        foreach (Renderer component in rendererComponents)
        {
	component.enabled = false;
        }

        foreach (Collider component in colliderComponents)
        {
        component.enabled = false;
        }

        Debug.Log("Trackable " + mTrackableBehaviour.TrackableName + " lost");
    }

}

 

How to pause / stop the audio when that object has appeared previously is missing from the camera view ?
And when the marker visible again, the audio will be resume / play back ?

ASK Unity AR Sound

December 17, 2013 - 9:43am #2

Yea that shoud be pretty easy,

 

just listen(Dellegate or whatever) to the 2 Methods TrackingLost and TrackingFound you can find in the DefaultTrackableBehaviour Script, and when one of those is called, handle it and play the sound...

Greetings 

Paatz :)

Log in or register to post comments