I'd like to know how to give the proper speed to fly a Unity engine airplane using physical operations.

Asked 1 years ago, Updated 1 years ago, 308 views

The presentation code is the script that is attached to the airplane.I'd like to fly an airplane using physics.
If you press the space key in this state, it will not move, but if you press the space and press the cross key, the coordinates will be strange like in the image shown.Why is this?

I would like to know the proper implementation method to provide speed when flying an airplane by physical operation.

Enter a description of the image here

Reference site: https://sites.google.com/view/ronsu900/createfs/wing1

using System.Collections;
using System.Collections.General;
using UnityEngine;

public class control:MonoBehavior

{

    // Wing area wing area [m^2]

    public floatingArea;

    Rigidbody rigidBody;

    void Start()
    {
        rigidBody=GetComponent<RigidBody>();
    }

    void Update()
    {
        Vector3vec = new Vector3(0,0,0);

        //vec.z=-1;

        // Move to the left
        if(Input.GetKey(KeyCode.LeftArrow))
        {
            vec.x = 1.0f;
        }
        // Move to the right
        if(Input.GetKey(KeyCode.RightArrow))
        {
            vec.x = -1.0f;

        }
        // Move forward
        if(Input.GetKey(KeyCode.UpArrow))
        {
            vec.y = -1.0f;

        }
        // Move Back
        if(Input.GetKey(KeyCode.DownArrow))
        {
            vec.y = 1.0f;
        }

        // advance
        if(Input.GetKey(KeyCode.Space))
        {
            vec.z = 1.0f;
        }


        rigidBody.velocity=vec;
    }


    void FixedUpdate()
    {




        // Convert to local vector Convert to local vector

        Vector3 localVelocity = this.transform.InverseTransformVector(rigidBody.velocity);



        // Speed Speed

        float v=rigidBody.velocity.magnitude;



        // Get Angle-of-attack (pitch) from local vector Calculate the angle of attack (pitch angle) from the local vector

        floataoa=-Mathf.Atan2(localVelocity.y,localVelocity.z)*Mathf.Rad2Deg;



        //------------------------

        // ▼ Lift

        //------------------------



        // Lift coefficient (simple version)

        float cl = aoa*0.1f;



        // Calculate lift lift

        float lift = CalculateLiftOrDrag(cl,wingArea,v);



        // Calculate lift direction (normalized) vector lift vector

        Vector3 liftVector=Vector3.Cross(Vector3.Cross(rigidBody.velocity, this.transform.up), rigidBody.velocity).normalized;



        // Apply lift lift

        rigidBody.AddForce(liftVector*lift);



        //------------------------

        // ▼ drag

        //------------------------



        // Drag coefficient drag coefficient (simple version)

        float cd = Mathf.Clamp01 (0.005f + Mathf.Pow (Mathf.Abs(aoa)*0.0315f,5));



        // Calculate drag drag

        floatdrag=CalculateLiftOrDrag(cd,wingArea,v);



        // Calculate drag direction (normalized) vector drag vector

        Vector3dragVector=-rigidBody.velocity.normalized; // Just normalizing the inverse vector of the velocities



        //------------------------

        // ▼ Apply force

        //------------------------



        // Apply drag lift

        rigidBody.AddForce(dragVector*drag);

    }





    // COMMON METHOD FOR CALCULATING LIFT AND RESISTANCE

    public static float CalculateLiftOrDrag (float coefficient, float surface, float velocity, float airDensity=1.293f)

    {

        float q=0.5f*(velocity*velocity)*airDensity;// dynamic pressure

        return q*surface*coefficient;

    }

}

c++

2022-11-05 19:29

1 Answers

There was a problem with the location of the collision.
I just had an appropriate square collision just below me because it was troublesome, but when I put the capsule in the front and the back of the wheel, the position didn't go wrong.


2022-11-05 19:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.