简体   繁体   English

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

[英]How can I pause an navmesh agent moving in between waypoints and then continue?

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();
    }
}

If the agent while moving between the waypoints is distance smaller then 3 from one of the random picked npcs slowly stop the agent but fast enough not to pass the npc distance smaller then 3 then both npc and agent should rotate smooth facing each other.如果代理在路点之间移动时距离小于 3,则从随机挑选的 NPC 之一开始缓慢停止代理,但速度足够快,不会通过小于 3 的 NPC 距离,则 NPC 和代理都应面向彼此平滑旋转。

After rotation part is over and they are facing each other do something.旋转部分结束后,他们面对面做一些事情。 After this "Do Something" part is over make the agent rotating smooth back facing to where it was and then move him again to continue moving the waypoints.在这个“做某事”部分结束后,让代理平滑地旋转回到原来的位置,然后再次移动他以继续移动航点。 I want to stop him and rotate....but it's more like pause him the agent rotate do stuff and then continue moving between the waypoints.我想阻止他并旋转....但这更像是暂停他,代理旋转做一些事情,然后继续在航路点之间移动。

Each time the agent visit npc call it a pause of the agent.每次代理访问 npc 调用它的代理暂停。 The logic is to pause the agent and continue.逻辑是暂停代理并继续。

-Stop Navmesh Agent script -停止导航网格代理脚本

Transform destinationPoint=(create and store temporary destination point)
gameObject.GetComponent<NavMeshAgent>().Stop();

-then rotate object manually in slow motion and do other stuff - 然后以慢动作手动旋转 object 并做其他事情

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 then setting back to destination point - 然后返回目的地

gameObject.GetComponent<NavMeshAgent>().SetDestination(destinationPoint);

I think this will help you;)我认为这会对你有所帮助;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM