为了账号安全,请及时绑定邮箱和手机立即绑定

Unity如何使游戏对象无休止地通过航路点?

Unity如何使游戏对象无休止地通过航路点?

C#
千万里不及你 2021-04-29 18:11:13
我有以下代码:using UnityEngine;using UnityEngine.AI;using System.Collections;public class AutoPilot : MonoBehaviour {public Transform[] points;private int destPoint = 0;private NavMeshAgent agent;void Start () {    agent = GetComponent<NavMeshAgent>();    // Disabling auto-braking allows for continuous movement    // between points (ie, the agent doesn't slow down as it    // approaches a destination point).    GotoNextPoint();}void GotoNextPoint() {    // Returns if no points have been set up    if (points.Length == 0)        return;    // Set the agent to go to the currently selected destination.    agent.destination = points[destPoint].transform.position;    // Choose the next point in the array as the destination,    // cycling to the start if necessary.    destPoint = (destPoint + 1) % points.Length;}void Update () {    // Choose the next destination point when the agent gets    // close to the current one.    if (!agent.pathPending && agent.remainingDistance < 2f)        GotoNextPoint();}}我经过航路点的地方,代理跟踪它们,但是当我到达最后一个航路点时,如何使它重新启动?继续前进?可以通过某种方式重置还是最好的方法是什么?
查看完整描述

1 回答

?
忽然笑

TA贡献1806条经验 获得超5个赞

我通过添加以下内容修复了该问题:


if (destPoint == points.Length) {

        destPoint = 0;

    }


查看完整回答
反对 回复 2021-05-08
  • 1 回答
  • 0 关注
  • 133 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信