Inside of my combat logic for my AI I use coroutines to time the behaviour. Basically I start a random coroutine containing one behaviour, wait, then stop the coroutine and repeat. However the stop coroutine part does not work for one behaviour which has another coroutine inside. This results in stacking those coroutines over time. Thus after a while I get an almost constant stream of bullets.
While i was able to work arround this issue by merging both coroutines I am still curious to find out why my initial approach did not work. I am still learning so every help at improving my comprehension is highly appreciated. Thanks in advance.
The essential parts of my code are the following:
function Start(){
Combatlogic();
}
function CombatLogic() : IEnumerator{
while(true){
if(!sleeping){
yield ChooseBehaviour(healthRatio);
}
yield;
}
}
function ChooseBehaviour(modi : float) : IEnumerator{
//some code
}else if (** randomized condition **){
//Debug.Log("Used Fire");
StartCoroutine( "executeShootNeedle");
yield WaitForSeconds(needleFireTime);
StopCoroutine( "executeShootNeedle");
}
//some more code
}
function ShootNeedle() : IEnumerator{
yield WaitForSeconds(needleAim);
//attack code
yield WaitForSeconds( needleDelay);
}
function executeShootNeedle() : IEnumerator{
while(true){
fireTransform.LookAt(target.position);
yield ShootNeedle();
}
}