The Attack state

If the player tank is close enough to attack our AI tank, we will rotate the turret object to face the player tank, and then start shooting. Then, if the player tank goes out of range, the tank goes back to the Patrol state:

 protected void UpdateAttackState() { //Set the target position as the player position destPos = playerTransform.position; //Check the distance with the player tank float dist = Vector3.Distance(transform.position, playerTransform.position); if (dist >= 200.0f && dist < 300.0f) { //Rotate to the target point Quaternion targetRotation = Quaternion.LookRotation(destPos - transform.position); transform.rotation = Quaternion.Slerp( transform.rotation, targetRotation, Time.deltaTime * curRotSpeed); //Go Forward ...

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.