x


control script for airplane

Hi guys, I have a problem with my script for an airplane. All I want to do is to rotate the plane around two axis with "awsd" and move it with space bar. Everything works until I rotate it around "x" and then it gets stuck in 90 degreese and cannot move anymore.

Here is the code:

var moveSpeed= 60.0;
var turnSpeed = 40.0;

function Update () {
    if(Input.GetButton("Forward"))
    {
       transform.eulerAngles.z += - turnSpeed * Time.deltaTime;
    }
    if(Input.GetButton("Backward"))
    {
       transform.eulerAngles.z += turnSpeed * Time.deltaTime;
    }

    if(Input.GetButton("Left"))
    {
       transform.eulerAngles.x +=  turnSpeed * Time.deltaTime;
    }
    if(Input.GetButton("Right"))
    {
       transform.eulerAngles.x += - turnSpeed * Time.deltaTime;
    }

    if(Input.GetButton("Jump")){
       transform.position += transform.right * moveSpeed * Time.deltaTime;
    }

}

Thanks for any help.

more ▼

asked May 19 '11 at 06:05 PM

efilD gravatar image

efilD
1 1 1 1

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

Here is my advice: Don't access position directly. A plane is often simulated by applying forces to the varying plane parts like rudders and wings. For a simplified arcade style feel you can think of the forces as a 'sum' acting on the rigid body as a whole

There are 4 forces on a plane if you take a summed view of things: Weight, thrust, lift, and drag. Drag is always in the opposite direction of velocity, thrust is always in the forward facing direction of the plane, weight is always world down, and lift is pointed in the wings up direction. So to make a plane roll left/right you addRelativeTorque to the plane. Here is my script for a plane and you can make due with what it does, I removed a lot of speed caps and 'constant management' for you to add your own, but this is the 'guts' of it. I added a 5th side slip force for look & feel, and I would urge you to take it out for the time being. There are some things to keep in mind as well such as penalty forces that make the plane nose dive or roll when banking. Note this is a controller for a jet and not really a WW2 plane.

//************************************************************
// Use PhysX rigidbody to update forces acting on a plane
//************************************************************
public void UpdateForces()
{
    elapsed += Time.deltaTime;

    //************************************************************
    // Update Roll & Yaw
    //************************************************************
    float ax = 0;
    float ay = 0;


    //************************************************************
    // Accelerometer controls, cater to specific situations
    //************************************************************
    if ( FlightStick.gameObject.active )
    {

         torqueY = 0.1f * FlightStick.NormalizedX * FlightManager.Data.Handling;

         torqueX = 400 * FlightStick.NormalizedY * (1-FlightManager.Data.Stability);

       EngineThrust += 25 * ThrustStick.NormalizedY;
       if( EngineThrust <= 0 )
       {
         EngineThrust = 0;
       }
       else if ( EngineThrust >= MaxThrust )
       {
         EngineThrust = MaxThrust;    
       }     
    }

    //************************************************************
    //4 Major forces on Plane: Thrust, Lift, Weight, Drag
    //************************************************************
    Vector3 airSpeedVector = -rigidbody.velocity;

    float angleOfAttack = -Mathf.Deg2Rad * Vector3.Dot( rigidbody.velocity, transform.up);
    float slipAoA = -Mathf.Deg2Rad * Vector3.Dot( rigidbody.velocity, transform.right);
    float speedWeight = AirDensity * ((CurrentSpeed*CurrentSpeed)/2);
    float weightCo = Weight;   

    sideSlipCoefficient = 0.001f * slipAoA * speedWeight;
    liftCoefficient = WingArea * angleOfAttack * speedWeight;

    //************************************************************
    //drag goes from x -> 3x * % 
    //angle goes from -1 -> 1
    //************************************************************
    float horizonAngle = Vector3.Dot( transform.forward, Vector3.up);
    dragCoefficient = 2*AirDrag + 2 * AirDrag * horizonAngle;
    thrustCoefficient = EngineThrust;
    AoA = Vector3.Dot( transform.up, Vector3.up);


    //************************************************************
    // Pitch from lift, yaw & roll from sideslip
    //************************************************************
    rollCoEfficient = -(torqueY+ay) * RollRate * speedWeight;
    pitchCoEfficient =  (torqueX+ax) * ElevatorRate * speedWeight; 
    yawCoEfficient = 0;

    pitchCoEfficient += liftCoefficient * 0.001f;
    yawCoEfficient += -sideSlipCoefficient * 2f;
    rollCoEfficient += -sideSlipCoefficient * 0.1f;

    //************************************************************
    // Calc vectors now we have coefficients
    //************************************************************
    WeightVector = Vector3.down * weightCo;


    ThrustForceVector = transform.forward * thrustCoefficient;     
    LiftVector = transform.up * liftCoefficient;
    DragVector = airSpeedVector * dragCoefficient;
    SideSlip = Vector3.right * sideSlipCoefficient;

    RollTorque = Vector3.forward * rollCoEfficient;
    YawTorque = Vector3.up * yawCoEfficient;     
    PitchTorque = Vector3.right * pitchCoEfficient;

    //************************************************************
    // Apply forces
    //************************************************************
    rigidbody.AddForce(ThrustForceVector, ForceMode.Force);
    rigidbody.AddForce(DragVector, ForceMode.Force);
    rigidbody.AddForce(LiftVector, ForceMode.Force);
    rigidbody.AddForce(WeightVector, ForceMode.Force);
    rigidbody.AddForce(SideSlip, ForceMode.Force);

    rigidbody.AddRelativeTorque(RollTorque);
    rigidbody.AddRelativeTorque(YawTorque);
    rigidbody.AddRelativeTorque(PitchTorque);

    //************************************************************
    // Instrumentation data & debug output
    //************************************************************
    VelocityVector = rigidbody.velocity;

    PlaneLift = liftCoefficient;
    //AoA = angleOfAttack;
    RotX = transform.rotation.eulerAngles.x;
    RotY = transform.rotation.eulerAngles.y;
    RotZ = transform.rotation.eulerAngles.z;
    torqueX = 0;
    torqueY = 0;
    CurrentSpeed = VelocityVector.magnitude;
    CurrentFuel -= Time.deltaTime * Weight/5 * EngineThrust/MaxThrust * 0.3f * FuelConsumption;

}
more ▼

answered May 19 '11 at 09:28 PM

Jeston gravatar image

Jeston
419 27 31 34

This is a much better solution than mine, rigidbodies will provide much more accuracy than a simple get/set of position. A jet script will work just fine for prop driven aircraft, just turn the speed down.

May 19 '11 at 09:33 PM ckfinite
(comments are locked)
10|3000 characters needed characters left

What you are experiencing is something called "gimbal lock", caused by the use of the euler rotation. Use transform.Rotate instead, or alter the quaternion directly.

more ▼

answered May 19 '11 at 08:03 PM

ckfinite gravatar image

ckfinite
1.3k 4 9 24

(comments are locked)
10|3000 characters needed characters left

thanks guys I'll try

more ▼

answered May 20 '11 at 09:38 AM

efilD gravatar image

efilD
1 1 1 1

(comments are locked)
10|3000 characters needed characters left

Hi Jeston, I have looked up your script but cant understand FlightStick gameObject. Dont you use input manager for stick input? is there any different way for joy inputs via game object? or am I being ridiculous :)

more ▼

answered Jan 29 at 11:49 AM

allpay gravatar image

allpay
1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x140
x68

asked: May 19 '11 at 06:05 PM

Seen: 2863 times

Last Updated: Feb 08 at 10:37 AM