Hey guys, I am working on a system for changing weapons.
I need it to wait for the disarm animation to finish before playing the arm animation for the next weapon. However it does not make it to the Debug.Log statement as it exits the routine after the yield. Any other ideas on how to get the routine to wait twice?
using UnityEngine;
using System.Collections;
public class weaponManager : MonoBehaviour {
private bool busy = false;
public Transform currentWeapon;
public Transform[] weapons;
public int startWeapon = 1;
void Start(){
StartCoroutine(EquipWeapon(startWeapon));
}
void Update(){
if(!busy){
if(Input.GetKeyDown(KeyCode.Alpha1)){
StartCoroutine(EquipWeapon(1));
}else if(Input.GetKeyDown(KeyCode.Alpha2)){
StartCoroutine(EquipWeapon(2));
}else if(Input.GetKeyDown(KeyCode.Alpha3)){
StartCoroutine(EquipWeapon(3));
}
}
}
IEnumerator EquipWeapon(int weaponNumber){
busy = true;
if(currentWeapon != null && currentWeapon != weapons[weaponNumber - 1]){
Transform weapon;
weapon = currentWeapon;
weapon.GetComponent<weaponScript>().armed = false;
weapon.animation.Play("Disarm");
currentWeapon = weapons[weaponNumber - 1];
yield return WaitForAnimation(weapon);
Debug.Log("passed");
weapon = currentWeapon;
weapon.animation.Play("Arm");
yield return WaitForAnimation(weapon);
weapon.GetComponent<weaponScript>().armed = true;
}else if(currentWeapon == null){
Transform weapon = weapons[weaponNumber - 1];
weapon.animation.Play("Arm");
yield return WaitForAnimation(weapon);
weapon.GetComponent<weaponScript>().armed = true;
}
busy = false;
}
private static IEnumerator WaitForAnimation(Transform obj){
do{yield return null;}while(obj.animation.isPlaying);
}
}