Eu até tentei usando o seguinte código:
private WaitForSeconds timeAttackWait;```
``` void Start()
{
timeAttackWait = new WaitForSeconds(attackWait);
StartCoroutine(CallVillainAtack());
}```
IEnumerator CallVillainAtack()
{
while (true)
{
yield return timeAttackWait;
VillainAtack();
}
}```
void VillainAtack()
{
if (player != null && Vector3.Distance(transform.position, player.transform.position) <= distatack && catchit)
{
anim.SetBool("Attack", true);
attack = true;
}
else if (player != null && Vector3.Distance(transform.position, player.transform.position) > distatack && catchit)
{
anim.SetBool("Attack", false);
attack = false;
}
if (attack)
{
agent.speed = 0;
agent.isStopped = true;
}
else
{
agent.speed = speedagent;
agent.isStopped = false;
}
}```
BUT it waits 3 secs to START the attacks and I want it waits 3secs for EACH attack. How Can I do this?
That looks to me like it should already wait 3 seconds each attack. How are you testing it? Note that the animation flag “Attack” will remain true for the entire 3 seconds between attacks.
Oh well… I’m testing pressing play to run my project ^.^
But how can I attack each three seconds?
Try replacing:
Yield return timeAttackWait;
With:
Yield return WaitForSeconds(attackWait);
Im not sure if it resets the timer each time its called if you store it & reuse it.
No, I mean how are you counting the number of attacks? Your function there doesn’t appear to write a log message or take any other player-visible discrete action.
I am theorizing that you have successfully set up your VillainAttack() function to run every 3 seconds, but that you have messed up your animations where you meant for the attack animation to play once but you actually set the attack animation to loop continuously.
There’s tons of examples online of people recommending that you reuse Wait objects in this way to avoid unnecessary memory allocations. You could test it, but I’m pretty sure that’s not the problem.
Guys! I found a sollution… I create another script, because thins is script is in a empty gameobject and this new script needs to be in the animated object. In this script, I create the EndAttack() where I call it in the end of the animation using event there ans solvev my problem. Thank you guys