Unity 2D绳索摆动物理算法问题:玩家卡在摆动中途
Unity 2D绳索摆动物理异常问题
我在Unity 2D中实现了绳索摆动平台游戏的物理逻辑,当前代码如下:
void FixedUpdate() { Vector2 testPosition = playerRigidbody.position + playerRigidbody.velocity * Time.deltaTime; Hooked(testPosition); } private void Hooked(Vector2 testPosition) { Vector2 t = new Vector2(tetherPoint.position.x, tetherPoint.position.y); Debug.DrawLine(tetherPoint.position, playerRigidbody.position); float currentLength = (testPosition - t).magnitude; if (currentLength < tetherLength) { currentLength = (playerRigidbody.position - t).magnitude * Time.deltaTime; } else currentLength = tetherLength; if ((testPosition - t).magnitude > tetherLength) { Vector2 x = (testPosition - t).normalized; testPosition = new Vector2(x.x * currentLength, x.y * currentLength); playerRigidbody.velocity = (testPosition - playerRigidbody.position) * Time.deltaTime; playerRigidbody.position = testPosition; } }
问题现象
- 向下摆动时功能正常,但玩家向上摆动时会卡在半空中,无法落到摆动弧线最低点
- 从高处落下时,摆动无法将玩家推到另一侧较高位置
- 从锚点另一侧放下玩家,会停在半空仅下落一半
- 编辑器中手动移动玩家,会被拉向某个固定位置
已排查操作
- 确认过
Time.deltaTime相关逻辑 - 运行时监控变量变化,仍未定位问题根源
- 怀疑是原伪代码转C#时出现偏差,参考过同类问题但其他用户实现差异较大
更新尝试
改用AddForce和MovePosition实现,问题依旧,修改后的代码片段:
playerRigidbody.AddForce((testPosition - playerRigidbody.position) * Time.deltaTime); playerRigidbody.MovePosition(testPosition);
内容的提问来源于stack exchange,提问作者C.Deighton
相关产品推荐
相关产品推荐

