Search Unity

JetFighter Flying Problem

Discussion in 'Works In Progress - Archive' started by Arash, Apr 8, 2012.

  1. Arash

    Arash

    Joined:
    Apr 8, 2012
    Posts:
    1
    hi everybody,its my first post to this forum and im new to unity.
    Im working on a jetfighter project i;ve searched a lot and i found that i have to add four forces(Lift,Weight,Thrust and Drag) to my plane to move due to the rotation of my plane and simulate an airplane movement.
    I've the Fixed 3rd Person Camera and the Rotation but the problem is i couldn't addforce right to my plane and it moves weird.
    i will be appreciate that somebody tell me how can i add forces together right to make my jetfighter Fly.

    This is My Code that i've assigned to my JetFighter :

    var flyer : Transform;
    var flyerRigidbody : Rigidbody;



    static var maxSpeed : float = 1000;
    var speedConst : float = 500;



    //var gravityConst = 9.8; // An arbitrary gravity constant, there is no particular reason it has to be 9.8...

    var minSmooth : float = 0.5;
    var maxSmooth : float = 500;



    private var trueSmooth : float;
    private var smoothRotation : float;
    private var truePitch : float;
    private var trueRoll : float;
    private var trueYaw : float;
    private var trueThrust : float;


    // Rotation Variables, change these to give the effect of flying anything from a cargo plane to a fighter jet.


    var pitchConst = 200;
    var rollConst = 200;
    var yawConst = 200;
    var revertRotation : Quaternion ;




    function Start ()
    {


    Screen.showCursor = false;
    revertRotation = transform.rotation;

    }



    function Update ()
    {
    var pitch : float = -Input.GetAxis ("Pitch") * pitchConst;
    var roll : float = Input.GetAxis ("Roll") * rollConst;
    var yaw : float = Mathf.Clamp(-Input.GetAxis ("Yaw"),-1,1) * yawConst;


    pitch *= Time.deltaTime;
    roll *= -Time.deltaTime;
    yaw *= Time.deltaTime;



    // Smothing Rotations...
    if ((smoothRotation > minSmooth)(smoothRotation < maxSmooth))
    {
    smoothRotation = Mathf.Lerp (smoothRotation, trueThrust, (maxSpeed-maxSpeed)* Time.deltaTime);
    }
    if (smoothRotation <= minSmooth)
    {
    smoothRotation = smoothRotation +0.35;
    }/*
    if ((smoothRotation >= maxSmooth) (trueThrust < (maxSpeed)))
    {
    smoothRotation = smoothRotation -0.35;
    }*/
    trueSmooth = Mathf.Lerp (trueSmooth, smoothRotation, 5 * Time.deltaTime);
    truePitch = Mathf.Lerp (truePitch, pitch, trueSmooth * Time.deltaTime);
    trueRoll = Mathf.Lerp (trueRoll, roll, trueSmooth * Time.deltaTime);
    trueYaw = Mathf.Lerp (trueYaw, yaw, trueSmooth * Time.deltaTime);


    }

    function FixedUpdate()
    {
    transform.Rotate (truePitch,-trueYaw,trueRoll);
    //Debug.Log("pitch is :" + truePitch + "Yaw is :" + trueYaw + "roll is" + trueRoll);
    Debug.Log(transform.rotation);
    transform.rotation.y = Mathf.Lerp(transform.rotation.y,revertRotation.y,1.8 * Time.deltaTime);
    transform.rotation.z = Mathf.Lerp(transform.rotation.z,revertRotation.z,1.8 * Time.deltaTime);
    transform.rotation.x = Mathf.Lerp(transform.rotation.x,revertRotation.x,3.1 * Time.deltaTime);
    transform.rotation.w = Mathf.Lerp(transform.rotation.w,revertRotation.w,3.1 * Time.deltaTime);

    //Debug.Log(Mathf.Lerp(-transform.rotation.w,revertRotation.w,0.1 * Time.deltaTime));



    }