为关卡烘焙导航网格后,即可创建能够在场景中导航的角色了。我们将使用圆柱体构建原型代理,并将代理设置为运动状态。为实现此目的,需要使用导航网格代理 (NavMesh Agent) 组件和简单脚本。
首先,让我们创建角色:
现在已设置简单的导航网格代理来准备接收命令!
开始尝试使用导航网格代理时,很可能需要根据角色大小和速度调整代理的尺寸。
__导航网格代理__组件将负责角色的寻路和移动控制。在脚本中,导航的设置十分简单,只需设置所需的目标点:导航网格代理可从此处进行所有内容的处理。
// MoveTo.cs
using UnityEngine;
using UnityEngine.AI;
public class MoveTo : MonoBehaviour {
public Transform goal;
void Start () {
NavMeshAgent agent = GetComponent<NavMeshAgent>();
agent.destination = goal.position;
}
}
接下来,我们需要构建一个简单的脚本,通过此脚本可将角色发送到另一个游戏对象指定的目标,并构建一个用作移动目标的球体:
MoveTo.cs
) 并将其内容替换为以上脚本。总而言之,在脚本中,您需要获取对导航网格代理组件的引用,然后为了将代理设置为运动状态,只需将一个位置分配给其 destination 属性。导航操作方法部分提供了进一步的一些示例来说明如何使用导航网格代理解决常见的游戏问题。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.