Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 统一-合作计划的时间不准确,想要四处走动吗_C#_Unity3d_Transform_Coroutine - Fatal编程技术网

C# 统一-合作计划的时间不准确,想要四处走动吗

C# 统一-合作计划的时间不准确,想要四处走动吗,c#,unity3d,transform,coroutine,C#,Unity3d,Transform,Coroutine,我希望使用coroutines随时间移动对象。我想在2秒钟内从点A到点B获取一个对象。为了实现这一点,我使用了以下代码: IEnumerator _MoveObjectBySpeed() { while (TheCoroutine_CanRun) { if(myObject.transform.position.y <= UpperBoundary.position.y) { myObject.transform.po

我希望使用
coroutines
随时间移动对象。我想在2秒钟内从点A到点B获取一个对象。为了实现这一点,我使用了以下代码:

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;
}
其中
allDistance
是底部和顶部边界之间的距离

\u平滑度
是一个固定值。问题是,越大我得到这个值,越准确从下到上的时间。请注意,此处的小值表示移动更平滑。此平滑度是协同程序在移动
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秒下到达顶部,并将
\u平滑度设置为0.01时,
myObject
到达顶部的时间为1.67秒。当平滑度
0.2s时,实际到达顶部的时间为1.04s

那么,为什么这是如此不准确,如何使其工作良好

此平滑度是协同程序在移动myObject之间等待的时间

您所犯的错误是假设一个共同例程在执行之前等待最佳时间。相反,它可能在超时完成后的下一帧执行

假设希望平滑运动,则希望每帧移动对象(例如,在使用“yield return null”的更新或协同例程中)


注意:每帧的持续时间可能不同(考虑144fps和15fps),您可以在Time.deltaTime中发现这一点

检查重复问题答案中的
MoveObject
功能。Oh和
Time.deltaTime
不应使用
Time.fixeddeltime
WaitForSeconds
    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