# Navigation 簡單用法 (設定多個地點讓NPC循環走(或是隨機走)) ## 設置 > 導航系統(也就是Navigation),會建立在Terrain上,所以要先確定地板用的是Terrain喔! > * Windows >AI > Navigation(下圖) ![](https://i.imgur.com/u8YMRqK.png) * Inspector旁邊就會出現Navigation的操作介面(?) ![](https://i.imgur.com/7GIUffW.png) * 在Object可以選擇all/mesh/terrain,Hierachry會顯示可以選擇的東西,選擇之後Navigation Static要勾選起來,然後選擇Walkable/Not walkable ![](https://i.imgur.com/HzAlMQs.png) * 設定好後按下Bake ![](https://i.imgur.com/FXEEWGY.png) * 這就是可以走的部分 ![](https://i.imgur.com/2zIE4oH.png) ## 人物的設置 * 如果是NPC,可以直接掛一個walk的動畫上去(記得要開loop) * 增加一個Component:Nav Mash Agent ![](https://i.imgur.com/PDcgwi5.png) * 建立一個Script : NPCAI Nav ```csharp= using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; //記得要加 public class NPCATNav : MonoBehaviour { public GameObject destination; NavMeshAgent theAgent; void Start() { theAgent = GetComponent<NavMeshAgent>(); } void Update() { theAgent.SetDestination(destination.transform.position); } } ``` ```csharp= using System.Collections; using System.Collections.Generic; using UnityEngine; public class NPCdes : MonoBehaviour { public int pivopoint; private void OnTriggerEnter(Collider other) { if(other.tag == "NPC") { if (pivopoint == 5) { pivopoint = 0; } if (pivopoint == 4) { this.gameObject.transform.position = new Vector3(28.88f, 1.07f, -26.26f); pivopoint = 5; } if (pivopoint == 3) { this.gameObject.transform.position = new Vector3(35.78f, 1.07f, -34.47f); pivopoint = 4; } if (pivopoint == 2) { this.gameObject.transform.position = new Vector3(39.62f, 1.07f, -35.59f); pivopoint = 3; } if (pivopoint == 1) { this.gameObject.transform.position = new Vector3(36.54f, 1.07f, -39.59f); pivopoint = 2; } if(pivopoint == 0) { this.gameObject.transform.position = new Vector3(34.44f, 1.07f, -26.26f); pivopoint = 1; } } } } ```