Unity笔记 Project02 点击移动
coconutnut

Click To Move with NavMesh - Unity 3D

选中地面

窗口 → AI → 导航: Static & Walkable

烘焙 → Bake

Player加组件Nav Mesh Agent

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class Player : MonoBehaviour
{
NavMeshAgent agent;

// Start is called before the first frame update
void Start()
{
agent = GetComponent<NavMeshAgent>();
}

// Update is called once per frame
void Update()
{
if(Input.GetMouseButtonDown(0)){
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit, Mathf.Infinity)){
agent.SetDestination(hit.point);
}
}
}
}