I just finished lesson 56 of the third person combat and traversel course. I’m wondering how I can make sure that the enemy doesn’t just swing every time he’s within attacking range, but maybe swings at a more random interval. For instance he swings first, then waits 2 seconds, then swings again, then waits 1 second, then swings again, then wait 3 seconds and swing twice. How would we do this? Thank you!
The best place for this is in the PlayerChaseState()
In Enter(), set a random float for the current wait.
waitTime = Random.Range(.5f, 3f);
And in Tick, add to a timeInState variable
timeInState+=deltaTime;
And in the if clause to determine if the enemy is in range:
else if (IsInAttackRange() && timeInState>=waitTime)
That is awesome, Brian. Thank you. Works well. Now the only issue is that the enemy keeps running while he’s already in attack range. Ideally he’d stop, and maybe side step to circle you a bit before swining. Is that hard to accomplish?
Rather than putting the delay in the Chase State, you could create a new state
EnemyWaitToAttackState, and put the delay in there. If the player goes out of range, then have the enemy go to the EnemyChaseState, otherwise attack after the delay.
Its just that he doesn’t go out of range. Right now with those last few lines that you shared with me added to the script, the enemy keeps running up to the player, kinda just bumping into the player while the running animation keeps playing, running in place, and then swings every once in a while.
That’s where an EnemyWaitToAttack state kicks in. He’s waiting to attack. In Enter, set the timer, and call an idle animation (preferably an animation where the enemy is in an attack ready position. Movement is just Move(new Vector3(0,0,0)) to let the ForceReciever act.
Then the timer completes in Tick(), switch to EnemyAttack State.
With an EnemyWaitToAttack, he could go out of range if the player bravely ran away. Then he should switch to EnemyChaseState.