首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我怎么才能让每个智能体也回到原来的位置呢?

我怎么才能让每个智能体也回到原来的位置呢?
EN

Stack Overflow用户
提问于 2020-05-13 00:13:17
回答 1查看 56关注 0票数 0
代码语言:javascript
运行
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class AgentControl : MonoBehaviour
{
    public List<Transform> points;

    private int destPoint = 0;
    private NavMeshAgent agent;
    private Transform originalPos;

    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).
        agent.autoBraking = false;

        originalPos = transform;
        points.Add(originalPos);

        GotoNextPoint();
    }


    void GotoNextPoint()
    {
        // Returns if no points have been set up
        if (points.Count == 0)
            return;

        // Set the agent to go to the currently selected destination.
        agent.destination = points[destPoint].position;

        // Choose the next point in the array as the destination,
        // cycling to the start if necessary.
        destPoint = (destPoint + 1) % points.Count;
    }


    void Update()
    {
        // Choose the next destination point when the agent gets
        // close to the current one.
        if (!agent.pathPending && agent.remainingDistance < 1f)
            GotoNextPoint();
    }
}

该脚本附加到每个代理。

我有两个特工。第一个智能体有一个航点,第二个智能体有八个航点。两个智能体在环路中的路点之间移动。我希望其中一个路点也将是它们的起始原始位置,因此每个代理也将移动到他的第一个原始起始位置作为点的一部分。

我试着在开始的时候加上这个

代码语言:javascript
运行
复制
originalPos = transform;
points.Add(originalPos);

但这并没有改变任何事情。第一个智能体移动到他的一个航路点并停留在那里,第二个智能体在两个航路点之间循环,但没有起始位置。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-13 16:18:17

有很多原因可以避免你的代理离开它的初始位置:

1) if (!agent.pathPending && agent.remainingDistance < 1f)此行代码指示如果靠近目的地,则转到下一个点。所以如果你的初始位置在先例航点附近,初始位置将被分流。

2)如果你的初始位置不在烘焙的路上,它将总是被分流。

因此,在使用navmesh代理时,不要忘记在每个路点之间要有相对间隔的路点。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61756562

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档