Ive been using co-rutines to do this but it only works when I use GetButton, when I use GetButtonDown (what I actually want to use) it doesnt play the whole animation, it should its like its ignoring WaitForSeconds? it only plays like the first frame.
Please ignore the comments in the code, thats stuff Ive been testing
using UnityEngine;
using System.Collections;
public class pistol : MonoBehaviour {
public GameObject character;
public Transform Spine;
// Use this for initialization
void Start () {
audio.pitch = 2.5f;
character.animation["shoot_pistol"].AddMixingTransform(Spine,true);
character.animation["shoot_pistol"].layer = 1;
//character.animation["shoot_pistol"].speed = 0.4f;
//character.animation["shoot_pistolidle"].speed = 0.5f;
}
// Update is called once per frame
void Update () {
if (Input.GetButton("Fire1")){
StartCoroutine(PlayAnimation());
StartCoroutine(PlaySound());
}
else {
character.animation.Stop("shoot_pistol");
character.animation.Stop("shoot_pistolidle");
}
}
IEnumerator PlayAnimation(){
if (Input.GetAxis("Horizontal")==0){
character.animation.Play("shoot_pistolidle");
print ("disparando");
yield return new WaitForSeconds(character.animation["shoot_pistol"].length);
}
else {
//character.animation["shoot_pistol"].time = 0.0F;
character.animation.Play("shoot_pistol");
print ("disparando");
yield return new WaitForSeconds(character.animation["shoot_pistol"].length);
//character.animation["shoot_pistol"].time = 0.0F;
//yield return new WaitForSeconds(character.animation["shoot_pistol"].length);
}
}
IEnumerator PlaySound(){
if (!audio.isPlaying){
audio.Play();
yield return new WaitForSeconds(character.animation["shoot_pistol"].length);
//yield return new WaitForSeconds(0.03f);
}
}
}
//audio.clip.length
also I would like to know if you have any idea how can I do a snap to frame (animation.time = 0) after I finish the shooting animation and if you dont press Fire1 again in like 5 seconds then go back to the normal walking cycle.