- Home /
Coroutine is not working
Animation should be played every few seconds but it does not
if (Vector3.Distance(transform.position, enemy.position) <= throwRadius)
{
if (!threw)
{
animator.SetTrigger("Throw");
threw = true;
}
else
{
StartCoroutine(Wait());
}
}
}
IEnumerator Wait()
{
yield return new WaitForSeconds(5f);
threw = false;
}
animation is still playing without delaying it for a 5 seconds, just one after another.Code is in update func
Well im not sure exactly what the default state of threw
is, is it true or false. If it starts at false, then it sets the trigger 'Throw' to true and then sets threw to false
. Thats it. It doesnt run the else. If it starts at true, then it will wait for 5 seconds (nothing is happening in this time), then it will set threw to false. Thats it. Im not sure if this code is in an update() or in a loop, but if not, your animation wont play with a delay. You need to rework the logic of your code, but I cant do it for you because I dont know the full context of the script. Hope you figure it out @conguerror
@Llama_w_2Ls Thanks for response. I will try out your suggestion right away. and yes, code is in Update function, I wrote it in question
@Llama_w_2Ls So it delayed it only once. Afterwards same thing happened, animation was triggered one after another(I also turned off self transition). I put coroutine in while loop as well, it did not help. I want animation to be triggered once in few seconds. I have read forum post about it, did not find any solution.
Answer by wooolly · Aug 19, 2020 at 01:16 PM
I'd rework this to remove the coroutine entirely and use a simple countdown field in your class instead. lastThrown would be the countdown field and should start off at 0f (float).
if (lastThrown > 0f) {
lastThrown -= 1f * Time.deltaTime;
}
else if (within distance) {
trigger;
lastThrown = 5f; // preferably replace '5f' with a CONSTANT
}
The snippet above will do the same thing as your coroutine, yet is simpler. I don't know the overhead that coroutines come with, but I'd think this is certainly optimal too. I only use coroutines in specific circumstances.
p.s. if your animation is still replaying instantly, it's likely you've configured the animation incorrectly. Double check to make sure the animation isn't set to loop, and test playing the animation just once to start with (adding in some temporary code to play it once in the Start method or something) - make sure the trigger correctly leaves the animation state and returns to another state.
Damn! thanks, I will check it out now . I don't know why I did not think of it -__-.
Haha yeah sometimes it's easy to get sidetracked with one solution that other solutions don't come to $$anonymous$$d. I hope it works out alright.
It did. Thanks man. Indeed other solutions were out of my $$anonymous$$d
Answer by Aviryx · Aug 19, 2020 at 01:30 PM
A coroutine is typically it's own function that can be invoked - not something that is directly placed inside of Update().
// increase someInteger by 1 every five seconds.
public int someInteger;
private bool isWaiting;
void Update()
{
if (!isWaiting)
{
StartCoroutine(someCoroutine());
}
}
private IEnumerator someCoroutine()
{
isWaiting = true;
yield return new WaitForSeconds(5);
someInteger++;
isWaiting = false;
}