Search Unity

Airship flight controls

Discussion in 'Editor & General Support' started by Mr-Logan, May 17, 2013.

  1. Mr-Logan

    Mr-Logan

    Joined:
    Apr 13, 2006
    Posts:
    455
    Hi

    Over the last few weeks I've been trying to make a script that lets the player control an airship, I've tried multiple different ways but have yet to come up with something that feels good. I have however hit on an oddity in my current script and I can't seem to spot the problem.

    When you fly forwards and then turn and let go of the turn key the ship will start turning erratically.



    The test build is here:
    https://dl.dropboxusercontent.com/u/6338532/Starboard%20Skies/Starsboard%20skies.html

    The vertical slider is instability
    The horizontal slider is speed
    You control with wasd


    The script controlling the ship looks like this:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RealTest2 : MonoBehaviour {
    5.    
    6.     public Transform zepFront, zepBack;
    7.     public UISlider verticalSlider;
    8.     public UISlider horizontalSlider;
    9.     public float upwardsLift;
    10.     public float speed = 1;
    11.     public float turnSpeed = 1;
    12.    
    13.     public Transform leftFrontEngine, leftBackEngine, rightFrontEngine, rightBackEngine;
    14.    
    15.     public float correctionValue = 3;
    16.    
    17.     private float elevationDifference;
    18.    
    19.    
    20.     void FixedUpdate(){
    21.        
    22.         if(Input.GetKey(KeyCode.A)){
    23.             rigidbody.AddForceAtPosition(transform.forward * turnSpeed, rightBackEngine.position);
    24.             rigidbody.AddForceAtPosition(transform.forward * turnSpeed, rightFrontEngine.position);
    25.         }
    26.         if(Input.GetKey(KeyCode.D)){
    27.             rigidbody.AddForceAtPosition(transform.forward * turnSpeed, leftBackEngine.position);
    28.             rigidbody.AddForceAtPosition(transform.forward * turnSpeed, leftFrontEngine.position);
    29.         }
    30.        
    31.        
    32.         Vector3 forwardsMoment = Input.GetAxis("Vertical") * transform.forward * speed;
    33.         rigidbody.AddForce(forwardsMoment);
    34.        
    35.        
    36.         upwardsLift = -rigidbody.velocity.y;
    37.         rigidbody.AddForce(Vector3.up * upwardsLift,ForceMode.VelocityChange);
    38.        
    39.         rigidbody.AddForceAtPosition(Vector3.up * -elevationDifference * correctionValue, zepFront.position);
    40.         rigidbody.AddForceAtPosition(Vector3.up * elevationDifference * correctionValue, zepBack.position);
    41.     }
    42.    
    43.    
    44.     void Update() {
    45.         elevationDifference = (zepFront.position.y - zepBack.position.y);
    46.         elevationDifference *= 0.03333333333f;
    47.         verticalSlider.sliderValue = elevationDifference + 0.5f;
    48.        
    49.         horizontalSlider.sliderValue = -rigidbody.velocity.z * 0.01f;
    50.     }
    51. }
    52.  
    If I comment out lines 39 and 40 the problem goes away, however I need those two lines otherwise the airship will tilt as easily forward as it does to the sides.. despite the length...


    If you have better ideas on how to control the ship I wouldn't mind hearing them either, however I need it to be controlled using physics.