我可以在不漂移的情况下使用 RigidBody.Addforce 吗?



我正在尝试使用刚体创建一个飞行模拟器。 我正在使用AddForce使飞机加速。 但是,当旋转飞机时,会有很多漂移。我怎样才能阻止这种情况?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlaneController : MonoBehaviour
{
public float maxSpeed = 200f;
public float speed = 90.0f;
public Vector3 rotationSpeedY = new Vector3(0, 0, 40);
public Vector3 rotationSpeedZ = new Vector3(40, 0, 0);
public Rigidbody rb;

private void FixedUpdate()
{
rb.AddRelativeTorque(-Input.GetAxis("Horizontal") * rotationSpeedY * Time.deltaTime);
rb.AddRelativeTorque(Input.GetAxis("Vertical") * rotationSpeedZ * Time.deltaTime);
rb.AddRelativeForce(transform.forward * speed);
speed -= transform.forward.y * Time.deltaTime * 50.0f;
if (rb.velocity.magnitude > maxSpeed)
{
rb.velocity = rb.velocity.normalized * maxSpeed;
}
if (speed < 35.0f)
{
speed = 35.0f;
}
}
}

这样做的原因是你没有以任何方式对空气动力学进行建模。当真正的飞机倾斜时,机翼根据其迎角与行进方向提供升力,同时它们产生相反方向的阻力。

如果你想让你的飞机像现实世界一样反应,你必须计算这两个力,如果你不是在寻找基于机翼形状的精确空气动力学建模,这将非常简单。目前你的飞机表现得好像没有大气层。

最新更新