Creating the Chase state

If the player is seen by the enemy and is not within attacking distance, the enemy would run to attack the player. This state, in which the enemy runs towards the player with hostile intent, is the Chase state. There are two main exit conditions for this state. If the enemy reaches attacking distance, they should change from the Chase state to the Attack state. In contrast, if the player disappears from sight, the enemy should continue to chase as best as they can for a while and then give up the chase if, after an interval, the player still cannot be sighted. Refer to the following code sample 7-9:

01 //This coroutine runs when object is in chase state 02 public IEnumerator State_Chase() 03 { 04 //Set current state 05 ...

Get Mastering Unity Scripting now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.