Timing out the zombie's pursuit
By adding the time out, we allow the zombie to forget about the player after enough time has been spent out of range:
Add the following code to the list of variables at the top of the
zombie_nav_Start
script:var alertTimeOut : float = 5.0; var alertTimer : float;
The variable
alertTimeOut
defines the time in seconds that it will take the zombie to forget about the player. Keeping this variable exposed, makes it easy to adjust in the Inspector panel.The next variable,
alertTimer
, keeps track of the time that has passed.In the
Update
function, locate the followingif
statement:if(playerDistance <= attackRange && !attacking) { Attack(); }
Add the following line of code after
Attack()
:alertTimer = 0.0;
After the closing curly bracket of the statement, add the following code:
else if(playerDistance > alertDistance) { alertTimer += Time.deltaTime; if(alertTimer > alertTimeOut) { alerted = false; } }
The
else if
statement comes...