- Home /
Yield WaitForSecond is skipping after the first time.
When it repeats the function, it doesn't do the Yield, but if I go out of the range and go back into the range (See code for what I mean) it will do the yield, but if I stay in the range it just keep going and going without a pause.
function Update () {
//Follow function
var playerPos = player.transform.position;
dist = Vector3.Distance(playerPos,transform.position);
if (dist > 1.0 && attack == false) {
transform.position = Vector3.MoveTowards(transform.position, player.transform.position, Time.deltaTime*speed);
}
else {
attack = true;
if (attack == true) {
attackPlayer();
}
}
}
function attackPlayer() {
yield WaitForSeconds(1.2);
if( dist < 1.0 ) {
var pStats = player.GetComponent(playerStats);
pStats.pLife -= baseDamage;
pLife = pStats.pLife;
}
attack = false;
}
Just to make sure you understand what you're doing here. You start a new coroutine every frame. Each coroutine will wait 1.2 seconds and then does the attacking. When you come close enough you starting coroutines every frame and after 1.2 seconds the first started coroutine will attack. Since you started a new coroutine every frame, from now on every frame another coroutine will finish it's waiting time and attack.
Ah that one helped, thanks. I figured it was something like that, cause I tried to prevent that but I couldn't.
Answer by aldonaletto · Oct 20, 2013 at 02:10 AM
If you want to respect an interval of 1.2s between attacks, the logic is wrong. WaitForSeconds only stops coroutines, not Update - you should use the boolean attack in a different manner in order to inhibit attacks while the coroutine is running:
function Update(){
if (dist > 1.0) { // out of attack range: chase player
transform.position = Vector3.MoveTowards(transform.position, player.transform.position, Time.deltaTime*speed);
}
else // inside range: attack!
if (attack == false){ // but only if interval ended
attackPlayer();
}
}
function attackPlayer() {
attack = true; // attacking now - don't call attackPlayer!
var pStats = player.GetComponent(playerStats);
pStats.pLife -= baseDamage; // apply damage
pLife = pStats.pLife;
yield WaitForSeconds(1.2); // wait interval
attack = false; // interval ended: can attack again
}
But I want it to wait x seconds see if the player is still in attack range, if so then they get hit. Not an instance attack.
Just change a little the function attackPlayer :
function attackPlayer() {
attack = true; // attacking now - don't call attackPlayer!
yield WaitForSeconds(1.2); // wait interval
if (Vector3.Distance(transform.position, player.transform.position) < 1.0){
var pStats = player.GetComponent(playerStats);
pStats.pLife -= baseDamage; // apply damage
pLife = pStats.pLife;
}
attack = false; // interval ended: can attack again
}
Yeah thanks, after I read the comment in my question I understand what I was doing wrong, and you corrected it.
Answer by Bunny83 · Oct 20, 2013 at 02:35 AM
In such a case it's way easier to use just a coroutine without Update:
function Start()
{
var pStats = player.GetComponent(playerStats);
while(true)
{
var playerPos = player.transform.position;
if (Vector3.Distance(playerPos,transform.position) < 1.0)
{
yield WaitForSeconds(1.2);
while(Vector3.Distance(playerPos,transform.position) < 1.0)
{
pStats.pLife -= baseDamage;
pLife = pStats.pLife;
yield WaitForSeconds(1.2);
}
]
else
{
transform.position = Vector3.MoveTowards(transform.position, player.transform.position, Time.deltaTime*speed);
yield;
}
}
}
Just make sure you always yield each iteration or it will crash your game ;)
This is a better alternative: the while infinite loop works as an Update that can be delayed by WaitForSeconds (actually, a PosUpdate, since it's executed after Update and before LateUpdate).
Your answer
![](https://koobas.hobune.stream/wayback/20220613120247im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Walking sound change 0 Answers
Problem with yield waitForSeconds in for loop 0 Answers
Confused about Coroutines 2 Answers
How can I make a variable false for a period of time? 3 Answers