- Home /
How can I create a working and semi-realistic lift force?
I was experimenting with making a flight simulator in Unity Engine and was coming across a problem where the plane would suddenly disappear due to floating-point and "direction normalized" errors. I think that this error has something to do with my lift equation in my PlaneController Script. My reasoning for this is that the errors occur when I try to do a loop in the plane, however it works fine if the plane is moving at slow speeds. One thing that I noticed about my simulator is that an unrealistically large amount of lift is generated. Since the lift equation takes into account the angle of the plane in radians, it seems to me that trying to do a loop just makes the lift force insanely large. I was wondering if anyone had any ideas as to how I could improve my thrust, drag, or lift equations. I posted my plane controller script below. As a heads up, the thrust, lift, and drag forces are added in the Fixed update section at the bottom of the script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlaneController : MonoBehaviour {
private Rigidbody plane;
public float throttle;
public float maxThrust;
public float thrust;
public float WingArea;
public float WingSurfaceArea;
private float yawRotation;
private float airDensity;
public float throttleAcceleration;
// Use this for initialization
void Start () {
yawRotation = 0;
plane = this.GetComponent<Rigidbody>();
throttle = 0f;
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.UpArrow))
{
if (throttle >= 100) throttle = 100;
else throttle += 0.15f;
}
if (Input.GetKey(KeyCode.DownArrow))
{
if (throttle <= 0) throttle = 0;
else throttle -= 0.15f;
}
if (Input.GetKey(KeyCode.A))
{
this.transform.Rotate(new Vector3(0, 0, 0.60f), Space.Self);
}
if (Input.GetKey(KeyCode.D))
{
this.transform.Rotate(new Vector3(0, 0, -0.60f), Space.Self);
}
if (Input.GetKey(KeyCode.W))
{
this.transform.Rotate(new Vector3(-0.60f, 0, 0), Space.Self);
}
if (Input.GetKey(KeyCode.S))
{
this.transform.Rotate(new Vector3(0.60f, 0, 0), Space.Self);
}
if (Input.GetKey(KeyCode.Q))
{
yawRotation -= 0.15f;
if (yawRotation > -25f) this.transform.Rotate(new Vector3(0, -0.15f, 0), Space.Self);
if (yawRotation <= -25f) yawRotation = -25f;
}
if (Input.GetKey(KeyCode.E))
{
yawRotation += 0.15f;
if (yawRotation < 25f) this.transform.Rotate(new Vector3(0, 0.15f, 0), Space.Self);
if (yawRotation >= 25) yawRotation = 25f;
}
else if (!Input.GetKey(KeyCode.Q) && !Input.GetKey(KeyCode.E))
{
if (yawRotation < 0)
{
this.transform.Rotate(new Vector3(0, 0.15f, 0), Space.Self);
yawRotation += 0.15f;
}
if (yawRotation > 0)
{
yawRotation -= 0.15f;
this.transform.Rotate(new Vector3(0, -0.15f, 0), Space.Self);
}
}
}
void FixedUpdate()
{
plane.AddRelativeForce(Vector3.forward * thrust * 0.02f, ForceMode.Impulse);
float xzVelocity = Mathf.Abs(this.GetComponent<Rigidbody>().velocity.z) + Mathf.Abs(this.GetComponent<Rigidbody>().velocity.x);
plane.AddRelativeForce(Vector3.back * 0.02f * (1.229f * (xzVelocity + Mathf.Abs(this.GetComponent<Rigidbody>().velocity.y)) * (xzVelocity + Mathf.Abs(this.GetComponent<Rigidbody>().velocity.y))/2) * WingArea * 0.02f, ForceMode.Impulse);
plane.AddForce((Vector3.up * 2 * 3.141f * Mathf.Deg2Rad * -plane.rotation.x * (xzVelocity * xzVelocity * 1.229f/2) * WingSurfaceArea)* 0.02f * 2, ForceMode.Impulse);
if(thrust < maxThrust) thrust += throttleAcceleration * throttle / 100 * 0.02f;
}
}
Thanks for reading my post.
As a side note, it seems like I was right in assu$$anonymous$$g that it has to do with the lift force. When I started decreasing it the plane starting disappearing less often. Another problem this lift force seemed to be causing is throwing off my camera, this also started to get fixed from a weaker lift force.
Couple of thoughts, should xzVelocity be the magnitude of the velocity in the xz plane? I.e. you'd take the square root of the sum of the squares of the z and x components. This would reduce xzVelocity, which would in turn reduce your lifting force.
While running this in the editor, you should be able to click on the plane object and watch how the values change in the inspector on the right as you fly around. If your forces are really strong maybe you run so far out in the Unity coordinates that your object doesn't render properly or at all?
Your answer
Follow this Question
Related Questions
Why Do I Suddenly Have This Problem? 0 Answers
Could not start compilation, Win32Exception 1 Answer
Cursor.SetCursur overload error? (Fixed) 1 Answer
"SetDestination" can only be called on an active agent that has been placed on a NavMesh 1 Answer
How do i add RelativeForce in Z-Axis using InputManager and Input.GetAxis() method ? (read) 0 Answers