Search Unity

Question How to make wind not only affect rigidbodys?

Discussion in 'Scripting' started by Aviation_Simmer, Sep 15, 2022.

  1. Aviation_Simmer

    Aviation_Simmer

    Joined:
    Aug 30, 2021
    Posts:
    110
    I've been working a very long tome on my flight simulator now. Its time to add some winds... I've tried it with that script:
    Code (CSharp):
    1.     public float Windspeed;
    2.     [Range(0, 360)]
    3.     public float Windheading;
    4.     Rigidbody rb;
    5.     public GameObject[] Player;
    6.  
    7.     void Start()
    8.     {
    9.         Player = GameObject.FindGameObjectsWithTag("Player");
    10.  
    11.         foreach (GameObject player in Player)
    12.         {
    13.             rb = player.GetComponent<Rigidbody>();
    14.         }
    15.     }
    16.  
    17.     void FixedUpdate()
    18.     {
    19.         foreach (GameObject player in Player)
    20.         {
    21.             rb.AddForce(transform.forward * Windspeed);
    22.         }
    23.  
    24.         transform.localEulerAngles = new Vector3(0, Windheading, 0);
    25.     }
    the script works but it only affects the rigidbody, and not the wings. its like pushing the aircraft. (I mean that is was the script is doing...) but how can I add a wind which affects everything? so that even the wings generate lift?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    In the simplest sense of what you're doing above, a wind would just be a displacement each frame. I don't see anything remotely like lift being computed above.

    Remember, airfoils experience lift (force perpendicular to MAC (mean aerodynamic chord)) and drag (force in line with MAC) and torque (twisting moment around the lateral axis) as a result of relative wind and angle of attack.

    If you want wind to affect lift, then wherever you calculate lift based on angle of attack and relative wind, simply add the local wind to that before you compute the lift.

    I used a package from a fellow named gasgiant for my Pilot Kurt game. The package computes a nice robust flight simulator without being too computationally intensive. Here is my fork of his repo:

    https://github.com/kurtdekker/Aircraft-Physics

    Here is a good discussion video: