I’m using 2d animations to represent my ally units attacking enemies. The problem is when there is more than one of the same unit spawned into the scene their animation sometimes do not behave independently. I have it set so the unit starts moving once its enemy is dead and its animation is done. However, other units of the same kind will not move until all animations are finished. So even though one ally may have started and finished his attack before another of the same kind he waits util the other is finshed before moving on.
Here is the relevant code:
void TargetSearch(){
GameObject on = GameObject.FindGameObjectWithTag("enemytag");
if(on != null ){
target= on.transform;
xa = transform.position.x;
c = target.position.x;
//stop and shoot when within shooting distance along x axis
if(c<=xa+dist & sec == false ){//sec is for the gap in bullet firing
currentSpeed = 0;
if(currentSpeed == 0){AllyAnim.PlayAnim(1);audio.PlayOneShot(Uzi);}//else{AllyAnim.PlayAnim(0);}
sec = true;
StartCoroutine(WaitB());
}
}
IEnumerator WaitB(){
yield return new WaitForSeconds(1.25f);
if(c<=xa+dist){EnemyScript.Health--;}
sec = false;
}