You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 21:45:30