这里以2017.2.3f1为例
1.先打开导航
选中场景内的物体,勾选static
烘焙场景
给需要移动的目标物体NavMeshAgent组件,把该移动物体的NavMeshAgent组件拖拽给脚本的
public NavMeshAgent agent;
声明上面这条语句时,需要引入using unityEngine.AI.
脚本如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Hero : MonoBehaviour
{
public NavMeshAgent agent;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
//鼠标点击的位置
print(hit.point);
agent.SetDestination(hit.point);
}
}
}
}