繁体   English   中英

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

[英]Can I use RigidBody.Addforce without drifting?

我正在尝试使用刚体创建飞行模拟器。 我正在使用 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;
        }
    }
}

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM