【发布时间】:2017-11-13 04:44:29
【问题描述】:
所以我正在从教程中对光线投射物理处理程序进行测试。我从每个模拟的相同高度放下一个块,目标是让块以一个平滑的运动落在平坦的表面上。目前,我的光线投射通过皮肤宽度从盒子底部投射出来,光线长度从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;
}
我的问题是,当我一遍又一遍地运行这个模拟时,大约一半的时间,当块与平面碰撞时,它会在表面上方产生这个初始“停止力”,然后块上的重力会重新启动并将其直接拉到表面上。
所以基本上,有时它会在比其他时间更远的距离感测到碰撞,迫使它过早地在半空中停下来。我通过观看hit.distance 和velocity.y 上的Debug.Log 来绘制这一点。这种不利影响发生的时间,hit.distance 高于0.1 和velocity.y 大约是-9.56,而有利的时间hit.distance 低于0.1 和velocity.y 是-9.67。
当我完全不改变系统时,是什么导致了模拟高度和数字的这种差异?是编辑器的滞后还是需要考虑的问题?它会出现在移动应用上吗?
感谢您的帮助。
【问题讨论】:
-
你是在
Update()还是FixedUpdate()运行这个? -
你是否将光线长度乘以
time.deltaTime? -
@moonismadeofcheese 这是我用来计算光线长度的方法 "float rayLength = Mathf.Abs(velocity.y * Time.deltaTime) + skinWidth;" (也感谢您前几天回答我的问题)
-
@Draco18s 我在 Update() 中运行这些。我认为你在我忽略的东西上。我所有的物理动作都应该在 FixedUpdate() 中正确吗?这让我相信更新时间的不一致。deltaTime 是我所看到的罪魁祸首
-
@Draco18s 事实证明你对情况的分析是正确的。我愚蠢地将我的物理放置在 Update() 函数而不是 FIxedUpdate() 函数中,并且 time.deltaTIme 的变化导致了不一致的光线投射问题。我的错。您应该以完整答案的形式回复,我很乐意将其作为正确答案进行检查并投票。