设置一个胶囊提代替人物进行测试
添加组件 Nav Mesh Agent
添加代码控制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI; //现在导航在unity.AI中
public class textNav : MonoBehaviour
{
private NavMeshAgent agent;
private Ray ray;
void Start()
{
agent = transform.GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
//从摄像机发出到点击坐标的射线
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
//显示射线,只有在scene视图中才能看到
Debug.DrawLine(ray.origin, hit.point);
Debug.Log(hit.point);//碰撞坐标
agent.SetDestination(hit.point);
}
}
}
}
实现在game视图中点击,人物导航至点击位置。