03. Nav Mesh Agent - eungyukm/UnityNavMesh GitHub Wiki
-
Capsule에 Red Material을 드래그하여 Material을 적용합니다.
-
Capsule에 Nav Mesh Agent 컴포넌트를 적용합니다.
-
Nav Mesh Agent를 컨트롤 할 AIControl 스크립트를 생성합니다.
using UnityEngine;
using UnityEngine.AI;
public class AIControl : MonoBehaviour {
public NavMeshAgent agent;
// Use this for initialization
void Start () {
agent = GetComponent<NavMeshAgent>();
}
}
-
AI Control 스크립트를 부착합니다.
-
AgentManager를 생성 합니다.
-
AgentManager의 소스 코드를 작성합니다.
using UnityEngine;
public class AgentManager : MonoBehaviour {
GameObject[] agents;
// Use this for initialization
void Start () {
agents = GameObject.FindGameObjectsWithTag("AI");
}
// Update is called once per frame
void Update () {
if (agents == null)
{
return;
}
// 왼쪽 마우스를 누를 경우
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
// 카메라에서 마우스의 위치를 100거리 내의 마우스를 찾아 RaycastHit로 반환합니다.
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 100))
{
//
foreach (GameObject agent in agents)
agent.GetComponent<AIControl>().agent.SetDestination(hit.point);
}
}
}
}
- Capsule의 Tag를 AI라고 붙여 줍니다.