0

我一直在研究沿着我从 Navmesh Unity3d 获得的路径的对象移动我正在使用协程,在其中我使用 while 循环控制它

  public void DrawPath(NavMeshPath pathParameter, GameObject go)
{

    Debug.Log("path Parameter" + pathParameter.corners.Length);
    if (agent == null || agent.path == null)
    {
        Debug.Log("Returning");
        return;
    }

    line.material = matToApplyOnLineRenderer;
    line.SetWidth(1f, 1f);

    line.SetVertexCount(pathParameter.corners.Length);

    allPoints = new Vector3[pathParameter.corners.Length];

    for (int i = 0; i < pathParameter.corners.Length; i++)
    {
        allPoints[i] = pathParameter.corners[i];
        line.SetPosition(i, pathParameter.corners[i]);

    }

   StartCoroutine(AnimateArrow(pathParameter));

    //StartCoroutine(AnimateArrowHigh(pathParameter));
}


#endregion


#region AnimateArrows


void RunAgain()
{
    StartCoroutine(AnimateArrow(Navpath));
}

IEnumerator AnimateArrow(NavMeshPath path)
{
    Vector3 start;
    Vector3 end;

    while (true)
    {


        if (index > 0)
        {
            if (index != path.corners.Length - 1)
            {
                start = allPoints[index];

                index += 1;
                end = allPoints[index];

                StopCoroutine("MoveObject");
                StartCoroutine(MoveObject(arrow.transform, start, end, 3.0f));
                yield return null;

            }
            else
            {
                index = 0;
                RunAgain();
            }
        }
        else if (index == 0)
        {
            start = allPoints[index];
            arrow.transform.position = allPoints[index];

            index += 1;
            end = allPoints[index];


           StopCoroutine("MoveObject");
           StartCoroutine(MoveObject(arrow.transform, start, end, 3.0f));

            yield return null;
        }
    }


}

IEnumerator MoveObject(Transform arrow, Vector3 startPos, Vector3 endPos, float time)
{
    float i = 0.0f;
    float rate = 1.0f / time;
    journeyLength = Vector3.Distance(startPos, endPos);
            float distCovered = (Time.time - startTime) * speed;
            float fracJourney = distCovered / journeyLength;


    while (i < 1.0f)
    {


       // Debug.Log("fracJourney In While" + fracJourney);
        arrow.position = Vector3.LerpUnclamped(startPos, endPos, fracJourney);

        yield return endPos;
    }
    Debug.LogError("Outside While");
}

但问题是我必须以恒定的速度移动对象,但是我的对象在每个循环中都在加速,因为我必须在循环中进行移动,所以它往往会移动,直到用户想要通过输入结束它,请帮助我不明白我在协程中做错了什么,我的对象的速度正在上升,我希望它保持不变,但不知何故它不能那样工作,谢谢

4

3 回答 3

2

作为替代方案,您可以利用 Unity 的 AnimationCurve 类轻松绘制各种超平滑动画类型:

在此处输入图像描述

您可以在检查器或代码中定义曲线

 public AnimationCurve Linear
{
    get
    {
        return new AnimationCurve(new Keyframe(0, 0, 1, 1), new Keyframe(1, 1, 1, 1));
    }
}

您可以像这样在协程中定义 useage:

Vector2.Lerp (startPos, targetPos, aCurve.Evaluate(percentCompleted));

其中“percentCompleted”是您的计时器/TotalTimeToComplete。

从这个函数中可以看到一个完整的 lerping 示例:

    IEnumerator CoTween(RectTransform aRect, float aTime, Vector2 aDistance, AnimationCurve aCurve, System.Action aCallback = null)
{
    float startTime = Time.time;
    Vector2 startPos = aRect.anchoredPosition;
    Vector2 targetPos = aRect.anchoredPosition + aDistance;
    float percentCompleted = 0;
    while(Vector2.Distance(aRect.anchoredPosition,targetPos) > .5f && percentCompleted < 1){
        percentCompleted = (Time.time - startTime) / aTime;
        aRect.anchoredPosition = Vector2.Lerp (startPos, targetPos, aCurve.Evaluate(percentCompleted));
        yield return new WaitForEndOfFrame();
        if (aRect == null)
        {
            DeregisterObject(aRect);
            yield break;
        }
    }
    DeregisterObject(aRect);
    mCallbacks.Add(aCallback);
    yield break;
}

查看此 Tween 库以获取更多代码示例:https ://github.com/James9074/Unity-Juice-UI/blob/master/Juice.cs

于 2017-01-27T20:07:35.117 回答
1

while (i < 1.0f)将永远运行,因为iis0.0f0.0fis always< 1.0f并且在您的 while 循环中没有任何位置,您可以在其中递增i以使其 >= 1.0f。您需要一种方法来退出该 while 循环。它应该如下所示:

while (i < 1.0f){
i++ or i= Time.detaTime..... so that this loop will exist at some point.
}

你的移动功能也很糟糕。下面的函数应该做你想做的事情:

bool isMoving = false;
IEnumerator MoveObject(Transform arrow, Vector3 startPos, Vector3 endPos, float time = 3)
{
    //Make sure there is only one instance of this function running
    if (isMoving)
    {
        yield break; ///exit if this is still running
    }
    isMoving = true;

    float counter = 0;
    while (counter < time)
    {
        counter += Time.deltaTime;
        arrow.position = Vector3.Lerp(startPos, endPos, counter / time);
        yield return null;
    }

    isMoving = false;
}

此外,在您的AnimateArrow(NavMeshPath path)函数中,替换三行代码:

StopCoroutine("MoveObject");
StartCoroutine(MoveObject(arrow.transform, start, end, 3.0f));
yield return null;

yield return StartCoroutine(MoveObject(arrow.transform, start, end, 3.0f));

这样做将等待MoveObject函数完成,然后返回并在while循环中再次运行。你必须更换这些内部 if (index != path.corners.Length - 1)else if (index == 0)

于 2016-04-08T10:39:21.263 回答
0

也许你可以将速度乘以 0.95f。这将使其加速,然后保持恒定速度,然后当您希望它停止时,它会逐渐减速。增加0.95f将使其加速/减速更快。

于 2016-04-08T10:27:59.533 回答