The Chase state

Similarly, while the tank is in the Chase state, it checks its distance with the player tank. If it's close enough, it'll switch to the Attack state. If the player tank has gone too far, then it'll go back to the Patrol state:

 protected void UpdateChaseState() { //Set the target position as the player position destPos = playerTransform.position; //Check the distance with player tank When //the distance is near, transition to attack state float dist = Vector3.Distance(transform.position, playerTransform.position); if (dist <= 200.0f) { curState = FSMState.Attack; } //Go back to patrol is it become too far else if (dist >= 300.0f) { curState = FSMState.Patrol; } //Go Forward transform.Translate(Vector3.forward * Time.deltaTime ...

Get Unity Artificial Intelligence Programming - Fourth Edition 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.