- Home /
Is this possible ?
Hi, as below I have this while loop for if the player shoots the enemy at long range, it generates a random number for the enemy to chase the player for, but what I want to do is if the player shoots the enemy again at long range I want the chaseTime in the while loop to update with the new generated number without coming out of the loop and reseting the boolean longRangeShot to false, is this possible?
function shootingLongRange(){
longRangeShot = true;
chaseTime = Random.Range(8, maxChaseTime); // Random time to chase the Player
while(chaseTime > 0){
yield WaitForSeconds (1);
terroristChaseTime--;
yield;
}
longRangeShot = false;
}
Answer by TonyLi · Jun 06, 2013 at 05:03 PM
Sure. Just reset chaseTime in the loop. If the player shoots again, set longRangeShot=true again. In the code below, notice show setChaseTime() also resets longRangeShot.
var chaseTime : float = 0;
var longRangeShot = false;
function setChaseTime() {
chaseTime = Random.Range(8, maxChaseTime); // Random time to chase the Player
longRangeShot = false;
}
function shootingLongRange() {
setChaseTime();
while(chaseTime > 0) {
yield WaitForSeconds (1);
if (longRangeShot) {
setChaseTime();
} else {
chaseTime--;
}
yield;
}
This should do what you're asking, but you could also restructure the functions if this doesn't make sense with the semantics of what you're intending to do.
Your answer
![](https://koobas.hobune.stream/wayback/20220613103640im_/https://answers.unity.com/themes/thub/images/avi.jpg)