将旋转应用于刚体变换

问题描述 投票:0回答:1

我一直在研究滚球类游戏。我想在 X 轴上旋转刚体并在 Y 轴上进行刚体变换。我知道刚体旋转和变换旋转不是好朋友。应用旋转后会产生抖动效果。

请帮助我如何解决这个问题。

void FixedUpdate()
{
    if (applyBrakes) { rb.velocity = rb.velocity * 0.85f; return; }
    var localInput = moveHorizontal * cvCam.right +   moveVertical * cvCam.forward;
     var templocal = transform.InverseTransformDirection(localInput).normalized;
    m_TurnAmount =  Mathf.Atan2( templocal.x , templocal.z );
    localInput.y = 0;
    rb.angularVelocity += new Vector3(localInput.z , 0 , -localInput.x) * force * Time.fixedDeltaTime;
    print(m_TurnAmount * Time.fixedDeltaTime * 360);
    transform.Rotate(Vector3.up ,  m_TurnAmount * Time.fixedDeltaTime * 360 , Space.World );        
    if (rb.velocity.magnitude > maxVelocity)
    {
        rb.velocity = rb.velocity.normalized * maxVelocity;
    }
}

需要帮助来解决此问题。

c# unity-game-engine game-physics
1个回答
0
投票

当您使用物理时,您不应通过

Transform
分配或访问任何内容,而应始终仅通过
Rigibody

您可以尝试使用例如

,而不是通过
transform.Rotate

rb.MoveRotation(rb.rotation * (Quaternion.Inverse(rb.rotation) * Quaternion.Euler(0, m_TurnAmount * Time.fixedDeltaTime * 360, 0) * rb.rotation));

Transform.Rotate
复制
Space.World
的行为(参见 源代码

© www.soinside.com 2019 - 2024. All rights reserved.