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

Unity开发:飞机出现倒飞现象的问题求助

解决飞机类游戏倒飞问题

问题描述

开发躲避障碍物的飞机游戏时,出现飞机倒飞的异常现象。修改forwardAcceleration的正负值后问题仍未解决,初始位置与旋转均为Vector3(0,0,0),IDE无报错,此前运行正常。

原代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playerControllerX : MonoBehaviour
{
Rigidbody rb;
private float rollSpeed = 8f;
private float pitchSpeed = 3f;
private float horizontalInput;
public float forwardAcceleration = -5f;
public float rotateInput;
public float bankSpeed = 1.45f;

void Start()
{
    rb = GetComponent<Rigidbody>();
}

void Update()
{
   
  
}

void FixedUpdate()
{
rb.AddForce(Vector3.up * 2);
}

void LateUpdate()
{
    rotateInput = Input.GetAxis("Vertical") * pitchSpeed * Time.deltaTime;  
   horizontalInput = Input.GetAxis("Horizontal") * rollSpeed * Time.deltaTime;
    
    transform.Translate(Vector3.up * bankSpeed * Time.deltaTime);
    transform.Rotate(0,0,rotateInput * 10);
    transform.Rotate(horizontalInput * 5,0,0);
    transform.Translate(forwardAcceleration,0,0);
    
 }
}

问题根源

  1. 移动方向错误:当前沿世界X轴移动(transform.Translate(forwardAcceleration,0,0)),而非飞机自身的前进方向(Z轴),旋转后移动方向与机头朝向脱节,视觉上呈现倒飞。
  2. 旋转轴映射混乱:Vertical输入控制绕Z轴旋转(翻滚),而非常规的俯仰(绕X轴),导致飞机姿态逻辑异常。
  3. 移动方式冲突:同时使用刚体AddForce和Transform.Translate,两种控制方式相互干扰,引发运动逻辑混乱。

解决方案

方案1:用Transform直接控制(简化逻辑)

统一用Transform控制移动和旋转,禁用刚体物理模拟避免冲突:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playerControllerX : MonoBehaviour
{
    private Rigidbody rb;
    private float rollSpeed = 8f;
    private float pitchSpeed = 3f;
    public float forwardSpeed = 5f; // 重命名为前进速度,语义更清晰
    public float climbSpeed = 1.45f; // 重命名为爬升速度,对应垂直移动

    void Start()
    {
        rb = GetComponent<Rigidbody>();
        rb.isKinematic = true; // 启用运动学刚体,避免物理与Transform操作冲突
    }

    void LateUpdate()
    {
        // 获取输入
        float verticalInput = Input.GetAxis("Vertical");
        float horizontalInput = Input.GetAxis("Horizontal");

        // 垂直移动(爬升/下降)
        transform.Translate(Vector3.up * climbSpeed * Time.deltaTime);
        // 前进移动(沿飞机自身前方,确保方向与机头一致)
        transform.Translate(transform.forward * forwardSpeed * Time.deltaTime);

        // 俯仰控制(抬头/低头,绕X轴)
        transform.Rotate(verticalInput * pitchSpeed * Time.deltaTime * 10, 0, 0);
        // 翻滚控制(左右倾斜,绕Z轴,负号修正输入与翻滚方向匹配)
        transform.Rotate(0, 0, -horizontalInput * rollSpeed * Time.deltaTime * 5);
    }
}

方案2:用刚体物理控制(更符合物理规范)

若需保留物理效果,改用AddForce和AddTorque控制,移除所有Transform直接操作:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class playerControllerX : MonoBehaviour
{
    private Rigidbody rb;
    private float rollSpeed = 8f;
    private float pitchSpeed = 3f;
    public float forwardSpeed = 5f;
    public float climbSpeed = 1.45f;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        float verticalInput = Input.GetAxis("Vertical");
        float horizontalInput = Input.GetAxis("Horizontal");

        // 施加前进力(沿飞机自身前方)
        rb.AddForce(transform.forward * forwardSpeed);
        // 施加垂直爬升力
        rb.AddForce(Vector3.up * climbSpeed);

        // 施加俯仰力矩(绕X轴)
        rb.AddTorque(Vector3.right * verticalInput * pitchSpeed);
        // 施加翻滚力矩(绕Z轴,负号修正方向)
        rb.AddTorque(Vector3.forward * -horizontalInput * rollSpeed);
    }
}

修改说明

  • 移动方向改为transform.forward,确保飞机始终沿机头朝向前进,解决倒飞视觉问题。
  • 调整旋转轴映射,让Vertical输入控制俯仰(绕X轴)、Horizontal输入控制翻滚(绕Z轴),符合常规飞机操作逻辑。
  • 统一移动控制方式,避免刚体与Transform操作冲突,保证运动逻辑一致性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 20:57:04