首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何暂停在两个航路点之间移动的navmesh代理,然后继续?

如何暂停在两个航路点之间移动的navmesh代理,然后继续?
EN

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

public class NavigateAgent : MonoBehaviour
{
    public List<Transform> points = new List<Transform>();
    public List<GameObject> npcs;
    public NavMeshAgent agent;

    private int destPoint = 0;


    void Start()
    {
        var wayPoints = GameObject.FindGameObjectsWithTag("Waypoint");
        foreach (GameObject waypoint in wayPoints)
        {
            points.Add(waypoint.transform);
        }

        npcs = GameObject.FindGameObjectsWithTag("Npc").ToList();

        //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;

        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 VisitNpcs()
    {
        var npc = npcs[Random.Range(0, npcs.Count)];
        var distance = Vector3.Distance(npc.transform.position, agent.transform.position);

        if (distance < 3f)
        {
            // Stop slowly agent.
            // Rotate agent and the npc at the same time slowly smooth to face each other.
        }
    }

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

如果智能体在两个路点之间移动的距离小于3,则从随机挑选的npc之一开始慢慢停止智能体,但速度足够快,不能超过npc的距离小于3,那么npc和智能体都应该面向对方平滑旋转。

在旋转部分结束后,他们面对面做一些事情。在这个“做点什么”部分结束后,让智能体平滑地向后旋转到它原来的位置,然后再次移动他继续移动航点。我想阻止他和rotate....but,这更像是让他停下来,让特工轮流做些事情,然后继续在路点之间移动。

每次代理访问npc时,将其称为代理的暂停。逻辑是暂停代理并继续。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-07 22:04:31

-Stop Navmesh代理脚本

代码语言:javascript
运行
复制
Transform destinationPoint=(create and store temporary destination point)
gameObject.GetComponent<NavMeshAgent>().Stop();

-then在慢动作中手动旋转对象并执行其他操作

代码语言:javascript
运行
复制
IEnumerator RotateAnimation(float from, float to)
{
    float time = 0.02f;
    int speedOfRotation = 1;
    while (from < to)
    {
        yield return new WaitForSeconds(time);
        from += speedOfRotation;
        gameObject.transform.Rotate(0, speedOfRotation, 0);
    }

}

然后-and设置回目标点

代码语言:javascript
运行
复制
gameObject.GetComponent<NavMeshAgent>().SetDestination(destinationPoint);

我认为这将对您有所帮助;)

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

https://stackoverflow.com/questions/61650740

复制
相关文章

相似问题

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