Raycast或时间或滞后?

时间:2017-11-13 04:44:29

标签: unity3d unity3d-2dtools

所以我正在从一个教程中对一个raycast物理处理程序进行测试。我正在从每个模拟的相同高度下降一个块,目标是使块以平滑的方式落在平坦表面的顶部。目前,我的光线投影通过皮肤宽度从盒子的底部突出,raylength从velocity.y缩小,如下所示:

rayOrigin += Vector2.right * (verticalRaySpacing * i + velocity.x);
//Sends out the ray at its respective spot an length

RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.up * directionY, rayLength, collisionMask);
Debug.DrawRay(rayOrigin, Vector2.up * directionY * rayLength, Color.red);

//If the ray hits, make velocity = distance to the hit distance - skin width
//Also reset the rayLength for edge height discrepencies
if (hit)
{
    //Reports the very first hit.distance and velocity.y at the first hit
    if (!temp && hit.distance != 0)
    {
        Debug.Log(hit.distance);
        Debug.Log(velocity.y);
        temp = true;
    }

    velocity.y = (hit.distance - skinWidth) * directionY;
    rayLength = hit.distance;
}

我的问题是,当我一遍又一遍地运行这个模拟时,大约一半的时间当块与平面碰撞时,它会在表面上方产生一个初始的“停止力”,然后块上的重力重新启动并将其直接拉到表面上。

所以基本上,有时它会感觉到距离比其他时间更远的碰撞,迫使它太早停止空中。我通过观察Debug.Loghit.distance上的velocity.y来确定了这一点。发生这种不利影响的时间,hit.distance高于0.1velocity.y高于-9.56,有利于hit.distance的时间是某个地方低于0.1velocity.y-9.67

当我根本不更换系统时,模拟高度和数字的差异是什么?是编辑中的滞后还是需要考虑的事情?它会出现在移动应用程序上吗?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

评论证实,我的怀疑是真的:

您在Update()而不是FixedUpdate()中运行此代码,因此它不与物理引擎同步。差异(在子更新间隔)导致更新循环的代码提前执行,传递过多的力量,所以当物理模拟运行时(在FixedUpdate的时间表上),对象仍然是高和重力再次接管。

唯一的解决方案是将代码移至FixedUpdate()

相关问题