# Dragon flight simulator help!

Good day everyone! I’ve been trying to make a dragon flight simulator for the last couple of days until I ran into a couple of issues. So far I have made the gliding physics work referencing the wiki page: Lift (force) - Wikipedia however I’m having trouble figuring out how to make the dragon rotate properly so that the velocity changes accordingly too. I think the trick is using addForceAtPosition however it’s not quite working as I want it too. Is there something I’m doing wrong? Here is my code

``````#pragma strict

// The transform of the bones of the dragon that I might dinamicaly change later on
var wing_R : Transform;
var wing_L : Transform;
var tail : Transform;

// the surface area of the wings and force positions for each bodypart
var wingArea : Vector2;
var wing_R_ForcePos : Transform;
var wing_L_ForcePos : Transform;
var tail_ForcePos : Transform;

// Body
var mass : float;
var angularDrag : float;
var maxAngleOfAttack : float;

// Tail
var tailForce : float;
var tailRotateStrength : float;

function Start (){

// Set the rigidbody settings at the start
rigidbody.mass = mass;
rigidbody.angularDrag = angularDrag;

}

function FixedUpdate (){

// Most of the code bellow was refrenced to the wiki page of wing phisics
var v = rigidbody.velocity;
v -= Vector3.Project(v, transform.right);

// wing area * air density
var liftMult = wingArea.x * wingArea.y * 0.645;
var angleOfAttack = Vector3.Dot(Vector3.Normalize(transform.up), Vector3.up);
// Don't add force when the rotation of the dragon is more that 45
if (angleOfAttack < maxAngleOfAttack || angleOfAttack > 1){
angleOfAttack = 0;
}

// Rotate the dragon up and down however is very clunky and doesn't work as I want it to
if (Input.GetAxis("Vertical") > 0)
rigidbody.AddForceAtPosition (v.magnitude * tailForce * transform.up, tail_ForcePos.position);
if (Input.GetAxis("Vertical") < 0)

// Not working for some reason? :(
if (Input.GetAxis("Horizontal") > 0)
rigidbody.AddForceAtPosition (v.magnitude * tailForce * transform.up, wing_R_ForcePos.position);
if (Input.GetAxis("Horizontal") < 0)
rigidbody.AddForceAtPosition (v.magnitude * tailForce * transform.up, wing_L_ForcePos.position);

rigidbody.AddRelativeForce (liftMult * angleOfAttack * v.magnitude * transform.up);
// Very basic acceleration
rigidbody.AddRelativeForce (Input.GetAxis("Fire1") * 5000 * transform.forward);

//}

}

function OnGUI (){

// Debugging :smile:
GUILayout.Box ("Speed: " + rigidbody.velocity.z);
GUILayout.Box ("Upward force: " + rigidbody.velocity.y);
GUILayout.Box ("Rotation: " + transform.localEulerAngles.x);
GUILayout.Box ("Altitude: " + rigidbody.position.y);

}
``````

Have you tried using addtorque? I made a simple flight simulator once and this worked just fine.

I have however it’s not quite the effect I wanted to create. When I just added torque the dragon would rotate in a direction however the velocity would still be the same. The effect I’m trying to achieve is when you press the vertical keys the dragon rotates and his velocity changes accordingly. Right now you turn and the dragon just keeps flying sideways. I believe the way to do this properly is to use addForceAtPosition since it adds force and Torque. However I don’t know what position I should add the force. Should I add the force behind the dragon if I want to fly up. How much force should I use? Also I was thinking should I change the drag or angular drag of the rigidbody dynamically depending on the velocity of the dragon? It would make sense since it’s harder to turn an airplane that is traveling downwards very quickly.

I noticed you are applying the lift force in the vector3.up direction, is this intentional? the lift force in real life is normal to the wings (you can use the local up)

The gliding force of the dragon works correctly i’m just having trouble making the dragon turn correctly.

It doesn’t. You see, a plane turns because of lift force, it is not just for gliding. The lift is perpendicular to the wings, so if the plane rotates just a bit the lift force will turn the plane to the sides.

I’ve been looking for a controller for a dragon being used as a player, does anyone have anything that’s functional-thanks for the help.