简体   繁体   中英

Can I use RigidBody.Addforce without drifting?

I am trying to create a flight simulator, using rigid bodies. I am using AddForce to make the plane accelerate. However, when rotate the plane, there is a lot of drift. How can I stop this?

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;
        }
    }
}

The reason for this is that you're not modeling aerodynamics in any way. When a real plane banks the wings provide a lift force according to their angle of attack vs the direction of travel and at the same time they produce drag in the opposite direction.

If you want your plane to react like the real world you will have to calculate these two forces which will be pretty simple if you're not looking for accurate aerodynamic modelling based off of wing shape. At the moment your plane is acting as if there is no atmosphere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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