- Home /
multiple objects not behaving independently
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;
}
Answer by DaveA · Mar 01, 2012 at 09:48 PM
Seems like maybe all allys are finding the same enemy and waiting until it's dead? Is there more than one enemy at a time is really my question. How are you getting EnemyScript? FindWithTag will get the first one it finds. If you have more than one enemy, you should assign each enemy to specific allys (or vice-versa) so each ally knows who it's attacking and when it's dead.
Hi
There are multiple enemies spawned at the same time. There doesn't seem to be a problem picking up new enemies once the allies start moving again. The only delay is when one of the allies hasn't finished the attack animation even after the target is dead. $$anonymous$$y target search in essence is picking up any tagged enemies in range and performs an attack until the enemy is dead. There is no problems if I have multiple enemies or even multiple allies of different types its just when multiple versions of the same unit are spawned.
Sorry to waste your time I just realised I had another coroutine that interfered with the one I had set up to wait for the attack animation thanks anyway :)
Your answer
