Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How do i find the forward velocity of an object ? And other issues

Discussion in 'Physics' started by ZachMut, Dec 23, 2022.

  1. ZachMut

    ZachMut

    Joined:
    Mar 26, 2022
    Posts:
    5
    Hello !
    Right now, I try to figure out how to create a somehow realistic and physics-based airplane controller. Here is the script. Of course, it's a work in progress, but I'm already facing issues that Internet doesn't have any answers to.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AirplaneMovement : MonoBehaviour
    6. {
    7.     float lift;
    8.  
    9.     public float enginePower = 10f;
    10.     public float liftCoefficient = 1f;
    11.     public float coefficientMultiplier = 5f;
    12.     public float wingSurface = 20f;
    13.     public float airDensity = 1f;
    14.  
    15.     public float rollSensibility;
    16.     public float pitchSensibility;
    17.     public float yawSensibilty;
    18.  
    19.     Vector3 forward = velocity.normalized;
    20.  
    21.     float Throttle;
    22.     float Roll;
    23.     float Pitch;
    24.     float Yaw;
    25.  
    26.     Rigidbody rb;
    27.  
    28.     void Start(){
    29.         rb = GetComponent<Rigidbody>();
    30.     }
    31.  
    32.     void FixedUpdate()
    33.     {
    34.         liftCoefficient = (Mathf.Sin((Mathf.PI * transform.forward.y)/90) * Mathf.Cos(transform.forward.z)) * coefficientMultiplier;
    35.         lift = (airDensity * liftCoefficient * wingSurface * (forward * forward))/2;
    36.  
    37.         Throttle =  Input.GetAxis("Throttle") * Time.deltaTime;
    38.         Roll =      Input.GetAxis("Horizontal") * rollSensibility * Time.deltaTime;
    39.         Pitch =     Input.GetAxis("Vertical") * pitchSensibility * Time.deltaTime;
    40.         Yaw =       Input.GetAxis("Yaw") * yawSensibilty * Time.deltaTime;
    41.  
    42.         rb.AddForce(transform.forward * (Throttle * enginePower));
    43.         rb.AddForce(transform.up * lift);
    44.  
    45.         rb.AddTorque(transform.up * Yaw);
    46.         rb.AddTorque(transform.right * Pitch);
    47.         rb.AddTorque(transform.forward * -Roll);
    48.     }
    49. }
    50.  
    51.  
    At the start of the FixedUpdate() function, the lift coefficient is calculated knowing the AoA of the plane. It uses a sine function so lift will get higher under 45° and lower over 45°. That took some time to figure out, and I still doubt it's functional.

    For some reason, lift doesn't seem to apply during game testing. I don't know how to find the velocity of the plane. Another problem, it stops moving when gravity is enabled.
    Help !
    Thanks
     
  2. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    243
    Do you mean the local forward velocity? The forward velocity from the perspective of the plane?

    If so, here is what you can do:

    var planeForward = plane.forward; // You need the normalized forward direction!
    var localVelocity = planeForward * Vector3.Dot(planeForward, rigidbody.velocity);

     
  3. ZachMut

    ZachMut

    Joined:
    Mar 26, 2022
    Posts:
    5
    But there's no plane variable in my script.
     
  4. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    243
    The plane variable refers to your airplane. To which GameObject did you attach your AirplaneMovement MonoBehaviour? Depending on your answer the plane.forward can probably just be changed to transform.forward.
     
  5. ZachMut

    ZachMut

    Joined:
    Mar 26, 2022
    Posts:
    5
    Yes, the script is attached to the airplane GameObject. I see what you mean now. Thanks !
     
  6. ZachMut

    ZachMut

    Joined:
    Mar 26, 2022
    Posts:
    5
    I did some playtesting, and turns out that LocalVelocity being a Vector3, it can't be treated like a float. I just want it to be in the formula on line 35, the one that defines the lift. The forward variable should've been the speed of the airplane.
     
  7. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    243
    The magnitude of a velocity vector is called speed, so to get the speed you need to do this:

    var speed = localVelocity.magnitude;


    Note: You originally asked for the velocity and velocities in 3D are always vectors!
     
    ippdev likes this.
  8. ZachMut

    ZachMut

    Joined:
    Mar 26, 2022
    Posts:
    5
    But does this give me only the FORWARD component of the object's speed ? If the airplane goes downward (locally) I don't want this speed to be taken into account.
     
  9. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    243
    Yes, that's what it does. So if your local forward direction is the local z axis, you could also just write:

    var speed = Mathf.Abs(localVelocity.z);


    ...but as you can see, if your plane flies backwards, the speed variable would also be positive!