- Home /
loop animation while audio is playing
Hello,
I have an talk animation and some talk sound files. What i want is that the talk animation plays as long as the audio file is playing. The problem is that when the sound is shorter than the animation, the animation stops and go to idle but the sound is still playing. I want the animation to loop until the sound is finished.
I've tried the loop option and serval while creations (wich hangs the program).
Can someone push me in the good direction?
NB: i'm new at unit, still a noob ;) The code so far:
using UnityEngine; using System.Collections;
public class CatScript : MonoBehaviour {
Animator anim;
AudioSource phrase1Audio;
AudioSource phrase2Audio;
AudioSource phrase3Audio;
AudioSource phrase4Audio;
AudioSource phrase5Audio;
AudioSource[] audios;
// Use this for initialization
void Start () {
anim = GetComponent<Animator> ();
audios = GetComponents<AudioSource> ();
phrase1Audio = audios [0];
phrase2Audio = audios [1];
phrase3Audio = audios [2];
phrase4Audio = audios [3];
phrase5Audio = audios [4];
}
// Update is called once per frame
void Update () {
var audioIndex = Random.Range(0,audios.Length);
// Debug.Log (audioIndex);
if (Input.GetKey(KeyCode.Space))
{
audios[0].Play();
if (audios[0].isPlaying) {
anim.Play ("Talk Cat");
Debug.Log ("audio is playing");
}
}
if (!audios[0].isPlaying){
anim.Play ("Idle");
Debug.Log ("Audio not playing");
}
}
}
Comment