- Home /
Can I add a delay function into an independent function but not in the update?
What should I do if I want to add a delay function after case AIState.attacking?
Can I add a delay function into an independent function but not in the update?
E.g. I want to shoot the bullet object (let me call it X) 3 times, which would be like this:
X_X_X (symbol "_" representing a delay, let it be 0.5 second)
Let LoopA = "X_X_X"
I want to have this loop for 3 times, so it should be like this:
X_X_X - X_X_X - X_X_X, where the symbol " - " representing another delay I need to have.
I've already made this to be a function(let it be function Y) that will be called by switch cases.
Therefore, I need to add delay function into function Y. What should I do?
Here is the code:
function MakeDecision(){
switch(state){
case AIState.wandering:
Wander();
break;
case AIState.chasing:
Chase();
break;
case AIState.attacking:
// add delay here
Attack();
Comment
Best Answer
Answer by CoryButler · Sep 24, 2018 at 02:26 PM
Use WaitForSeconds() to delay the bursts of bullets and the firing of each bullet in a burst.
private void MakeDecision()
{
switch(state)
{
case AIState.wandering:
Wander();
break;
case AIState.chasing:
Chase();
break;
case AIState.attacking:
AttackAfterDelay(3, 0.5);
break;
default:
break;
}
}
// Delay the first attack (in this case, firing 3 bullets).
// Use same delay between attacks.
private IEnumerator AttackAfterDelay(int numAttacks, int delay)
{
while (numAttacks > 0)
{
yield return new WaitForSeconds(delay);
FireBullets(3, 0.5)
numAttacks--;
}
}
// Fire 1 bullet immediately. Then delay the firing of all following bullets.
private IEnumerator FireBullets(int numBullets, int delayBetweenBullets)
{
while (numBullets > 0)
{
FireBullet();
numBullets--;
yield return new WaitForSeconds(delayBetweenBullets);
}
}
// Fire a single bullet.
private void FireBullet()
{
// TODO: Fire a single bullet.
}
where am i put the $$anonymous$$akeDecision()? I put it in the upDate function but it is not work...
You may need to add another AIState. Try adding AIState.startAttack. In $$anonymous$$akeDecision(), change AIState.attacking to AIState.startAttack. Then, in AttackAfterDelay(), start by setting state to AIState.attacking, and end the method by setting state to whatever is appropriate (AIState.wandering or AIState.chasing);
private void $$anonymous$$akeDecision()
{
switch(state)
{
case AIState.wandering:
Wander();
break;
case AIState.chasing:
Chase();
break;
case AIState.startAttack: // Start the attack.
AttackAfterDelay(3, 0.5);
break;
case AIState.attacking:
// Do not add anything here. AttackAfterDelay() handles this AIState.
break;
default:
break;
}
}
// Delay the first attack (in this case, firing 3 bullets).
// Use same delay between attacks.
private IEnumerator AttackAfterDelay(int numAttacks, int delay)
{
state = AIState.attacking; // The attack has started. Don't allow a new attack until this one is done.
while (numAttacks > 0)
{
yield return new WaitForSeconds(delay);
FireBullets(3, 0.5)
numAttacks--;
}
state = AIState.chasing; // Or whatever AIState is appropriate. Just let the program know the attack is over.
}