Need audio to keep playing when the image is not tracked

I have a simple system of image tracking made with Unity. There is an animated avatar with audio source, but I need the users to be able to “start” the animation themselves, only while the image is detected. So I wrote a very simple script attached to a button that starts animation and audio with 2 different functions.

What I need is for the animation is to pause when the image is not tracked (which I am trying to achieve by setting anim.speed to 0) but I also need to the audio clip to keep playing on background.

How can I achieve this?
My code so far:

public void PlayAnimation()

{

    //m_animation.Play();

    if (m_animation != null && m_animation.gameObject.activeSelf)

    {

        //m_audioSource.clip = m_audioClip;

        m_animation.Play();

        //m_audioSource.Play();

    }

    else if (!m_animation.gameObject.activeSelf)

    {

        m_clip.speed = 0;

    }

}

public void PlaySound()

{

   

    //m_audioSource.Play();

    m_audioSource.clip = m_audioClip;

    if (m_animation != null && m_animation.gameObject.activeSelf)

    {

        m_audioSource.Play();

    }

Hi alberto.bonanni,

Rather than using one button to do both of this , which minimizes your control over he separate functions you can use the built-in settings of our image tracking target.

Using the On Seen and On Not Seen settings(see image below) you can initiate the audio when the image target is seen and continue it playing when also not seen.

You can still use the button to start the animation alongside these settings to pause it when the image target is not seen and they can resume by clicking the start button again. Unless you just want it to continue when the image target is seen again with no user input.

Unity-seen-events

Hope this helps.

Cheers,
Dapo

1 Like