AIController.cs (725B)
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using UnityEngine.AI; 5 6 public class AIController : MonoBehaviour { 7 8 public Transform target; 9 public Transform target2; 10 public float dist; 11 NavMeshAgent enemyNav; 12 13 private bool atTarget1 = false; 14 void Start () { 15 enemyNav = GetComponent<NavMeshAgent>(); 16 } 17 18 void Update () { 19 if(atTarget1){ 20 enemyNav.SetDestination(target2.position); 21 } 22 if(!atTarget1){ 23 enemyNav.SetDestination(target.position); 24 } 25 if(!atTarget1){ 26 dist = Vector3.Distance(target.position, transform.position); 27 }if(atTarget1) 28 { 29 dist = Vector3.Distance(target2.position, transform.position); 30 } 31 if(dist < 1f) { 32 atTarget1 = !atTarget1; 33 } 34 } 35 }