【发布时间】:2017-07-04 03:09:05
【问题描述】:
所以我有一个脚本来移动游戏对象。当我移动一个游戏对象时,它很流畅。但是当我第二次移动它时,它的移动速度非常慢,看起来有点马车。
移动脚本中的第一个输入是对象,然后是它需要移动到的位置和速度作为最后一个参数。所有坐标都基于本地位置。我使用等待是因为我想在执行第二个动作之前等待。
我也尝试过两次移动其他对象,但它们最终都移动得很慢/有问题。
我不想在更新中运行它,这就是我使用协程的原因。
这是我的代码:
IEnumerator MovementGentryOne()
{
StartCoroutine(Movement(GentryOne, MovementCoords.GentryOneBasin, gentryspeed));
yield return new WaitForSeconds(2);
StartCoroutine(Movement(GentryOneArm, MovementCoords.GentryArmMoved, gentryspeed));
yield return new WaitForSeconds(2);
StartCoroutine(Movement(GentryOnePicker, MovementCoords.GentryPickerPick, gentryspeed));
yield return new WaitForSeconds(4);
//this one is not working smooth.
StartCoroutine(Movement(GentryOnePicker, MovementCoords.GentryPickerStart, gentryspeed));
yield return null;
}
private IEnumerator Movement(GameObject toMove, Vector3 position, float time)
{
float elapsedTime = 0;
while (elapsedTime < time)
{
toMove.transform.localPosition = Vector3.Lerp(toMove.transform.localPosition, position, (elapsedTime / time));
elapsedTime += Time.deltaTime;
yield return null;
}
toMove.transform.localPosition = position;
}
有人知道出了什么问题吗?
亲切的问候
【问题讨论】:
-
您能否在开始新的运动协程之前确认一个运动协程已经完成?否则你会因为运动而争吵,这会导致奇怪、不流畅和迟钝的运动。
-
你可以使用 yield return StartCoroutine 而不是在中间等待几秒钟吗?
-
协程好像没有停止。使用调试器,我注意到它在 yield return null 之后永远不会到达语句
-
最大的问题是:如何和在哪里你是从哪里调用MovementGentryOne的? GentryOne、GentryOneArm 和 GentryOnePicker 是彼此的孩子吗?
-
我在下面的答案中解决了这个问题。是的,他们都是彼此的孩子。该方法是从 start 方法调用的。
标签: c# unity3d coroutine gameobject lerp