【问题标题】:Coroutine call stacks协程调用栈
【发布时间】:2017-01-03 12:54:10
【问题描述】:

我想使用我的协程平滑地插入多个游戏对象的位置和旋转,并附加脚本。当我启动协程时,平滑部分工作正常,但我所有的对象都移动到同一个位置,这不是我想要的。我想了解为什么会这样,以及是否有聪明的方法来处理它。

这就是我的协程的样子:

    IEnumerator interpolate_Plate(Transform targ)
{
    float passedTime = 0;

    while (!transform.Equals(targ))
    {
        passedTime += Time.deltaTime;

        transform.position = Vector3.Lerp(transform.position, targ.position, passedTime);
        transform.rotation = Quaternion.Lerp(transform.rotation, targ.rotation,passedTime);
        yield return null;
    }

    yield break;
}

我正在考虑创建一个包含列表的主协程,然后调用平滑部分。 问题只是,对于堆栈上的所有协程调用,targ 的引用总是被重置吗?

根据您的要求调用协程的函数:

public void move_Plate(Transform newPosition)
{
  StartCoroutine(interpolate_Plate(newPosition));
}

【问题讨论】:

  • 你是怎么调用 interpolate_Plate 的?
  • 您必须展示如何启动协程。此外,您的最后一次休息是无用的,因为协程无论如何都会退出。
  • 无论如何,您可能不需要协程,在更新中移动逻辑可能足以满足您的需求。
  • 我在原帖中添加了协程调用。我使用协程,因为当我使用更新时,它并没有很好地平滑一切。基本上我想同时平滑地插入多个对象。所以它们几乎都是在同一时间完成的。
  • move Plate 是从另一个脚本调用的。只是为了完整

标签: c# unity3d coroutine


【解决方案1】:

好的,所以我找到了解决方案,因为 Unity 或 C# 可以使用指针等等问题。我不断更改所有对象的转换,因为我使用了指向下一个对象的转换的指针。但是我将那个对象移到了,所以它最终都在我移动的最后一个对象上。

如何预防:

我创建了一个新类来存储我的值,以便旧类的位置和旋转。我在我移动盘子的类中存储了一个实例。 我现在按照 cmets 中的建议从协程更改为更新方法。使用标志检查是否应该移动对象。然后我将它平稳地移动到新的位置。

代码:

private Trans MoveTo;
private bool move;

void Update()
{
    if (move)
    {
        float passedTime = 0;
        passedTime += Time.deltaTime;

        transform.position = Vector3.Lerp(transform.position, MoveTo.position, passedTime);
        transform.rotation = Quaternion.Lerp(transform.rotation, MoveTo.rotation, passedTime);
    }
}

似乎有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-12
    • 2018-08-06
    • 2022-12-05
    • 2019-01-14
    • 1970-01-01
    • 1970-01-01
    • 2018-06-13
    • 1970-01-01
    相关资源
    最近更新 更多