Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Airplane controls...

Discussion in 'Scripting' started by SquirrelCoder, Dec 21, 2017.

  1. SquirrelCoder

    SquirrelCoder

    Joined:
    May 21, 2015
    Posts:
    5
    I want to make an airplane that tilts a certain amount whenever it turns. It turns, but for some reason whenever it tilts it always gets stuck. Here's me script:

    public float flyingSpeed = 10f;
    public Text zValue;
    private float horizontal;
    // Use this for initialization
    void Start ()
    {
    //transform.eulerAngles = new Vector3(0, 0, 10);
    }

    // Update is called once per frame
    void Update ()
    {

    }
    void FixedUpdate ()
    {
    horizontal = Input.GetAxis ("Horizontal");
    if (horizontal != 0)
    {
    if (Mathf.Abs(gameObject.transform.localRotation.z) < 0.25f)
    {
    transform.RotateAround (this.transform.position, Vector3.up, Input.GetAxis ("Horizontal") * 90f * Time.deltaTime);
    transform.Rotate (0f, 0f, -Input.GetAxis ("Horizontal") * 1.5f);








    Thanks for the help.
     
  2. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    This is a simple ship basic movement script. Use Lerp and Slerp to have smooth transitions.
    Code (CSharp):
    1. private float horizontal;
    2.     float pitch = 40;
    3.     float rotY;
    4.     void FixedUpdate()
    5.     {
    6.         horizontal = Input.GetAxis("Horizontal");
    7.         float rotZ = horizontal * -pitch;
    8.         rotY += horizontal * 2;
    9.         transform.rotation = Quaternion.Euler(0, rotY, rotZ);
    10.     }
     
  3. SquirrelCoder

    SquirrelCoder

    Joined:
    May 21, 2015
    Posts:
    5
    Thanks for your reply @alexeu. It worked! When I tried to make it so that you can pull up and down (which did work) it stopped rotating around. Here's my script:

    public float flyingSpeed = 10f;
    private float horizontal;
    private float vertical;
    float pitch = 40;
    float rotY;
    float rotX;
    float rotZZ;
    void FixedUpdate()
    {
    horizontal = -Input.GetAxis("Horizontal");
    vertical = Input.GetAxis ("Vertical");
    float rotZ = horizontal * -pitch;
    float rotX = vertical * pitch;
    rotY += horizontal * 2;
    rotX += vertical * 2;
    transform.rotation = Quaternion.Euler(0f, rotY, rotZ);
    transform.rotation = Quaternion.Euler(rotX, 0f, rotZ);
    transform.position -= transform.forward * flyingSpeed * Time.deltaTime;
    }
    }

    Thanks
     
  4. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    Do you use a joystick ?
     
  5. SquirrelCoder

    SquirrelCoder

    Joined:
    May 21, 2015
    Posts:
    5
  6. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    Ok, your script moves the player backward and inverts horizontal axis.
    anyway, you don't need 2 rotation statments.
    Code (CSharp):
    1.         transform.rotation = Quaternion.Euler(rotX, rotY, rotZ);
    2.  
    PS:
    i'm not sure your plane will move up/down. if not use

    Code (CSharp):
    1.         transform.position -= transform.TransformDirection( Vector3.forward * flyingSpeed * Time.deltaTime);
    2.  
     
    Last edited: Dec 25, 2017
  7. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    finaly i could make a quick test. it works.
    Code (CSharp):
    1.     public float flyingSpeed = 100f;
    2.     float horizontal;
    3.     float vertical;
    4.     float pitchZ = 40, pitchX = 40;
    5.     float rotY;
    6.  
    7.  
    8.     void FixedUpdate()
    9.     {
    10.         horizontal = Input.GetAxis("Horizontal");
    11.         vertical = Input.GetAxis("Vertical");
    12.         float rotZ = horizontal * -pitchZ;
    13.         float rotX = vertical * pitchX;
    14.         rotY += horizontal * 2;
    15.         rotX += vertical * 2;
    16.  
    17.         transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(rotX, rotY, rotZ), Time.deltaTime);
    18.         transform.position += transform.forward * flyingSpeed * Time.deltaTime;
    19.     }
    20.  
    this for an almost plane "normal behavior" ... it depend on your own goal.

    merry christmas to all :):)
     
  8. SquirrelCoder

    SquirrelCoder

    Joined:
    May 21, 2015
    Posts:
    5
    Okay it's working now. Thanks @alexeu. Again, is there a way to not make he curve so tight?
     
  9. SquirrelCoder

    SquirrelCoder

    Joined:
    May 21, 2015
    Posts:
    5
    Nevermind @alexeu. Worked it out :). Thanks again