文章中有实现这样的效果,但是没有给出方法,小弟在此演示一下,实现想法来自于之前写过的切水果。。。
1.首先创建一个plane
2.添加direct light
3.添加两个cube,一个为man,一个为target
4.选择plane,Navigation窗口中勾选Navigation Static
5.选中man,添加Navmesh agent
下面给man和target分别添加脚本。。哇咔咔。。
man的,比较简单
[C#]
纯文本查看 复制代码
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
|
using
UnityEngine;
using
System.Collections;
public
class
man : MonoBehaviour {
private
NavMeshAgent manone;
public
Transform target;
// Use this for initialization
void
Start () {
manone=gameObject.GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void
Update () {
manone.SetDestination(target.position);
}
}
|
target的脚本
[C#]
纯文本查看 复制代码
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
using
UnityEngine;
using
System.Collections;
public
class
target : MonoBehaviour {
// Use this for initialization
void
Start () {
}
// Update is called once per frame
void
Update () {
if
(Input.GetMouseButton(0))
{
Ray ray=Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if
(Physics.Raycast(ray,
out
hit,100))
{
if
(hit.collider.tag==
"plane"
)
{
transform.position=hit.point;
}
}
}
}
}
|
最后不要忘记把H视图中的target赋值man脚本中的target哦。。赶紧试试吧。。。对了,要记得把target的Mesh Render属性勾选掉
在这里之所以不直接使用Input.mouseposition是为了方便后续的碰撞检测哦。。。

本文介绍如何在Unity中使用导航网格让角色自动寻路到鼠标点击的位置。通过创建场景元素并编写C#脚本来控制角色移动。

被折叠的 条评论
为什么被折叠?



