1.首先给物体添加NavMeshAgent导航组件
2.给物体添加脚本,内容如下:
private NavMeshAgent nav; void Start() { nav = GetComponent<NavMeshAgent>(); } void Update() { if (Input.GetMouseButton(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hitInfo; if (Physics.Raycast(ray, out hitInfo)) { nav.SetDestination(hitInfo.point); } } }