【问题标题】:How to make NavMesh Agent stop and then continue his movement如何让 NavMesh Agent 停止然后继续他的运动
【发布时间】:2020-09-05 06:55:49
【问题描述】:

我正在尝试让敌人巡逻系统,每一个守卫到达他的点,他停止 10秒,然后继续他的动作。我尝试将 Blend 树中的动画与 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]

【问题讨论】:

    标签: c# unity3d navmesh


    【解决方案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

    【讨论】:

    • 谢谢,我要试试这个
    • 对了,我忘了说,巡逻必须是无限的
    • 天哪,它有效,但并不完全。等待 10 秒后 - 他继续移动并到达第二点,但随后他只是以行走动画停留在那里。我做了这样的改变:而不是使用 agent.autoRepath = true;转到下一个点();我用过:agent.ResetPath(); destPoint = (destPoint + 1) % points.Length; agent.SetDestination(points[destPoint].position);
    • 你把“time=10000”改成“time=100000”了吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-12
    • 2011-10-16
    • 2020-08-26
    • 1970-01-01
    • 2022-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多