【问题标题】:How can I pause an navmesh agent moving in between waypoints and then continue?如何暂停在航点之间移动的 navmesh 代理然后继续?
【发布时间】:2020-05-07 05:46:57
【问题描述】:
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 之一慢慢停止代理,但速度足够快,不会通过小于 3 的 NPC 距离,那么 NPC 和代理都应该面向彼此平滑旋转.

旋转部分结束后,他们面对面做一些事情。 在这个“做某事”部分结束后,让代理平滑地旋转回到原来的位置,然后再次移动他以继续移动航点。我想阻止他并旋转....但这更像是暂停他,代理旋转做一些事情,然后继续在航点之间移动。

每次代理访问 npc 调用它的代理暂停。逻辑是暂停代理并继续。

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    -停止导航网格代理脚本

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

    -然后以慢动作手动旋转对象并做其他事情

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

    -然后返回目的地点

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

    我认为这会对你有所帮助;)

    【讨论】:

      猜你喜欢
      • 2020-09-05
      • 1970-01-01
      • 1970-01-01
      • 2016-04-25
      • 1970-01-01
      • 2012-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多