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

Unity中飞机旋转角度被限制在-90°至90°之间的问题求助

问题:飞机旋转角度被限制在-90到90度之间

我正在实现一套移动系统,玩家仅需控制飞机的旋转而非移动,飞机将持续向前飞行,但目前出现旋转角度始终被限制在-90到90度之间的问题。

以下是每帧FixedUpdate中调用的RotatePlayer方法:

private void RotatePlayer()
{
    Vector2 inputVector = _playerInputActions.PlayerMovement.Move.ReadValue<Vector2>();

    if (inputVector != Vector2.zero)
    {
            Vector3 rotationDirection = new Vector3(inputVector.x, 0f, 0f);
            var rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(rotationDirection), Time.deltaTime * 100f);
            Debug.Log(rotation);
            transform.rotation = rotation;
            //_playerRigidbody.AddRelativeTorque(Vector3.up * inputVector.x * _rotationSpeed * Time.deltaTime);
    }
}

解决办法

问题根源

Quaternion.LookRotation(rotationDirection)的作用是让物体的Z轴指向目标方向,而你传入的rotationDirection是X轴方向的向量(比如(1,0,0)或(-1,0,0))。当Z轴试图指向X轴正方向时,最多只能转到90度;指向X轴负方向时最多转到-90度,这就导致旋转被限制在这个区间内。

你需要的是飞机绕Y轴左右转向,而非让Z轴指向X轴,所以得换一种旋转控制方式。

方案1:直接控制Y轴欧拉角

这种方式直接修改物体Y轴的旋转角度,逻辑直观:

private void RotatePlayer()
{
    Vector2 inputVector = _playerInputActions.PlayerMovement.Move.ReadValue<Vector2>();

    if (inputVector != Vector2.zero)
    {
        // 获取当前Y轴旋转角度
        float currentY = transform.eulerAngles.y;
        // 根据输入更新目标Y轴角度
        float targetY = currentY + inputVector.x * Time.deltaTime * 100f;
        // 应用旋转,保持X、Z轴角度不变(避免飞机抬头低头)
        transform.rotation = Quaternion.Euler(transform.eulerAngles.x, targetY, transform.eulerAngles.z);
    }
}

方案2:用Rotate做相对旋转

通过Rotate方法直接绕Y轴旋转,代码更简洁:

private void RotatePlayer()
{
    Vector2 inputVector = _playerInputActions.PlayerMovement.Move.ReadValue<Vector2>();

    if (inputVector != Vector2.zero)
    {
        // 绕世界空间Y轴旋转,符合飞机左右转向的逻辑
        transform.Rotate(Vector3.up, inputVector.x * Time.deltaTime * 100f, Space.World);
    }
}

方案3:启用物理驱动旋转(注释掉的代码)

如果想用物理引擎模拟旋转效果,可以启用你之前注释的AddRelativeTorque代码,记得调整_rotationSpeed参数:

private void RotatePlayer()
{
    Vector2 inputVector = _playerInputActions.PlayerMovement.Move.ReadValue<Vector2>();

    if (inputVector != Vector2.zero)
    {
        _playerRigidbody.AddRelativeTorque(Vector3.up * inputVector.x * _rotationSpeed * Time.deltaTime);
    }
}

内容的提问来源于stack exchange,提问作者Luis

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 10:16:26