点击自动导航
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;//导入命名空间
public class NavigationTest : MonoBehaviour
{
private NavMeshAgent navMeshAgent;
private void Start()
{
navMeshAgent = gameObject.GetComponent<NavMeshAgent>();
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Transform parent = hit.collider.transform.parent;
if (parent != null )
{
navMeshAgent.SetDestination(hit.point);
}
}
}
}
}
点击鼠标左键时,开启射线检测,记录下当前鼠标点击的物体,将其设为导航目的地。