如何让 NavMesh Agent 停止然后继续他的运动

How to make NavMesh Agent stop and then continue his movement

我正试图让敌人 巡逻系统,在那里 evrytime guard 达到 他的目标,他 stopes10秒,然后继续他的动作。我已经尝试将来自 Blend tree 的动画与来自 NavMeshAgent.

的 isStopped 属性 相结合

编辑:我目前的脚本让特工移动到一个点,然后他停了一段时间,然后只播放行走动画,但他停留在一个地方。

public Transform[] points;
private int destPoint = 0;
public NavMeshAgent agent;
public Animator animator;
public int time;

void Start()
{
    agent = GetComponent<NavMeshAgent>();
    animator = transform.Find("Enemy").GetComponent<Animator>();

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


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].position;

    // Choose the next point in the array as the destination,
    // cycling to the start if necessary.
    destPoint = (destPoint + 1) % points.Length;
    //agent.speed = 1f;
    //animator.SetFloat("Blend", agent.speed);
}

void Update()
{
    if (agent.remainingDistance == 0f && time == 100000)
    {
        agent.speed = 1f;
        Debug.Log(agent.remainingDistance);
        animator.SetFloat("Blend", 1);
        GotoNextPoint();
    }
    else if (agent.remainingDistance <= 0.5f && agent.remainingDistance != 0f && time == 100000)
    {
        animator.SetFloat("Blend",0);
        agent.enabled = false;
        GotoNextPoint();
    }
    else if(animator.GetFloat("Blend") == 0)
    {
        time--;
    }

    if (time == 99000 && animator.GetFloat("Blend") == 0)
    {
        time = 10000;
        agent.enabled = true;
        agent.isStopped = false;
        animator.SetFloat("Blend", 1);
        agent.autoRepath = true;
        GotoNextPoint();
    }
}

我更改了几行代码,现在代理在第一次停止后移动,但第二次他停止在第二点,行走动画仍然有效,时间不递减

if (time == 99000 && animator.GetFloat("Blend") == 0)
    {
        time = 10000;
        agent.enabled = true;
        agent.isStopped = false;
        animator.SetFloat("Blend", 1);
        //New lines of code
        agent.ResetPath();
        destPoint = (destPoint + 1) % points.Length;
        agent.SetDestination(points[destPoint].position);
    }[enter image description here][1]

首先,我会使用"SetDestination"函数来设置下一个目的地。

最后你写道:

if (time == 99000 && animator.GetFloat("Blend") == 0)
{
    time = 10000;  *-> needs to be 100000, not 10000*
    agent.enabled = true;
    agent.isStopped = false;
    animator.SetFloat("Blend", 1);
    agent.autoRepath = true;
    GotoNextPoint();
}

您可以使用 "NavMeshAgent.ResetPath" 来重置路径,而不是使用 "autoRepath".

"ResetPath"之后,使用函数GotoNextPoint()里面的"SetDestination"

还有一件非常重要的事情-检查是否有不止一个点。 如果只有一个点,你的守卫就会原地踏步。

更多信息,建议查看Unity NavMeshAgent