Unity - 协同程序的时间不准确,想要绕过
Unity - coroutines got inaccurate with timing, want to get around
我希望使用 coroutines
随着时间移动对象。我想得到一个从点 A 到点 B 的对象,比说 2 秒 。为此,我使用了以下代码:
IEnumerator _MoveObjectBySpeed()
{
while (TheCoroutine_CanRun)
{
if(myObject.transform.position.y <= UpperBoundary.position.y)
{
myObject.transform.position = new Vector3(myObject.transform.position.x,
myObject.transform.position.y + Step, myObject.transform.position.z);
}
yield return new WaitForSeconds(_smoothness);
}
TheCoroutine_CanRun = true;
moveAlreadyStarted = false;
}
并且 Step
的计算方式类似于
private void CalculateSpeed()
{
Step = _smoothness * allDistance / timeToReachTop;
}
其中 allDistance
是底部和顶部边界之间的距离。
_smoothness
是固定值。问题是,更大我得到这个值,更准确时间从下到上。请注意,这里的小值意味着更平滑的运动。这种平滑度是协程在移动 myObject
之间等待的时间。
时间是这样测的:
void FixedUpdate()
{
DEBUG_TIMER();
}
#region DEBUG TIME
public float timer = 0.0f;
bool allowed = false;
public void DEBUG_TIMER()
{
if (Input.GetButtonDown("Jump"))
{
StartTimer();
}
if (myObject.transform.position.y >= UpperBoundary.position.y)
{
StopTimer();
Debug.Log(timer.ToString());
//timer = 0.0f;
}
if (allowed)
{
timer += Time.fixedDeltaTime;
}
}
void StartTimer()
{
timer = 0;
allowed = true;
}
void StopTimer()
{
allowed = false;
}
#endregion
结果是:
当我想让物体在1秒到达顶部并将_smoothness
设置为0.01时,时间myObject
到达顶部的时间是 1.67 秒。当_smoothness
为0.2s时,实际到达顶部的时间为1.04s.
那么为什么它如此不准确以及如何让它正常工作?
This smoothness is the time the coroutine waits in between moving the myObject
您犯的错误是假设 co-routine 在执行之前等待 完美 时间。相反,它可能会在超时结束后的下一个 帧 上执行。
假设您想要流畅的运动,您想要移动对象 每 帧(例如,在使用 'yield return null' 的更新或 co-routine 中)。
注意:每帧可能需要不同的持续时间(考虑 144fps 与 15fps),您可以在 Time.deltaTime 中发现这一点。 https://docs.unity3d.com/520/Documentation/ScriptReference/Time-deltaTime.html
我希望使用 coroutines
随着时间移动对象。我想得到一个从点 A 到点 B 的对象,比说 2 秒 。为此,我使用了以下代码:
IEnumerator _MoveObjectBySpeed()
{
while (TheCoroutine_CanRun)
{
if(myObject.transform.position.y <= UpperBoundary.position.y)
{
myObject.transform.position = new Vector3(myObject.transform.position.x,
myObject.transform.position.y + Step, myObject.transform.position.z);
}
yield return new WaitForSeconds(_smoothness);
}
TheCoroutine_CanRun = true;
moveAlreadyStarted = false;
}
并且 Step
的计算方式类似于
private void CalculateSpeed()
{
Step = _smoothness * allDistance / timeToReachTop;
}
其中 allDistance
是底部和顶部边界之间的距离。
_smoothness
是固定值。问题是,更大我得到这个值,更准确时间从下到上。请注意,这里的小值意味着更平滑的运动。这种平滑度是协程在移动 myObject
之间等待的时间。
时间是这样测的:
void FixedUpdate()
{
DEBUG_TIMER();
}
#region DEBUG TIME
public float timer = 0.0f;
bool allowed = false;
public void DEBUG_TIMER()
{
if (Input.GetButtonDown("Jump"))
{
StartTimer();
}
if (myObject.transform.position.y >= UpperBoundary.position.y)
{
StopTimer();
Debug.Log(timer.ToString());
//timer = 0.0f;
}
if (allowed)
{
timer += Time.fixedDeltaTime;
}
}
void StartTimer()
{
timer = 0;
allowed = true;
}
void StopTimer()
{
allowed = false;
}
#endregion
结果是:
当我想让物体在1秒到达顶部并将_smoothness
设置为0.01时,时间myObject
到达顶部的时间是 1.67 秒。当_smoothness
为0.2s时,实际到达顶部的时间为1.04s.
那么为什么它如此不准确以及如何让它正常工作?
This smoothness is the time the coroutine waits in between moving the myObject
您犯的错误是假设 co-routine 在执行之前等待 完美 时间。相反,它可能会在超时结束后的下一个 帧 上执行。
假设您想要流畅的运动,您想要移动对象 每 帧(例如,在使用 'yield return null' 的更新或 co-routine 中)。
注意:每帧可能需要不同的持续时间(考虑 144fps 与 15fps),您可以在 Time.deltaTime 中发现这一点。 https://docs.unity3d.com/520/Documentation/ScriptReference/Time-deltaTime.html