Search Unity

Flight Control

Discussion in 'Scripting' started by robloz, Nov 26, 2015.

  1. robloz

    robloz

    Joined:
    Apr 27, 2013
    Posts:
    10
    Hi, I am trying to coding a flight control script for a character without successful. I would like to move the character as the following video:

    https://www.youtube.com/watch?v=oSisqnGBPqY

    Basically, if you only press k , l or w, the character is going up, down or foward, otherwise the character is moving foward, rotating and looking at the right place at the same time.

    This is the script, any ideas are welcome:

    Code (CSharp):
    1. public class FlyMovement : MonoBehaviour
    2. {
    3.     /*
    4.     back: Vector3(0, 0, -1)
    5.     forward: Vector3(0, 0, 1)
    6.    
    7.     left: Vector3(-1, 0, 0)
    8.     right: Vector3(1, 0, 0)
    9.    
    10.     up: Vector3(0, 1, 0)
    11.     down: Vector3(0, -1, 0)
    12.    
    13.     one: Vector3(1, 1, 1)
    14.     zero: Vector3(0, 0, 0)
    15.      */
    16.     public float speed;
    17.  
    18.     private Vector3 GetMovementVector()
    19.     {
    20.         Vector3 result = Vector3.zero;
    21.         float angleRotation = 20.0f;
    22.  
    23.         if(Input.anyKey)
    24.         {
    25.             if (Input.GetKey(KeyCode.W))
    26.             {
    27.                 result = Vector3.forward;
    28.  
    29.                 if (Input.GetKey(KeyCode.D))
    30.                 {
    31.                     result = Quaternion.AngleAxis(-1 * angleRotation, Vector3.up) * result; // y = 1
    32.                 }
    33.                 else if (Input.GetKey(KeyCode.A))
    34.                 {
    35.                     result = Quaternion.AngleAxis(angleRotation, Vector3.up) * result; // y = 1
    36.                 }
    37.  
    38.                 if (Input.GetKey(KeyCode.J))
    39.                 {
    40.                     result = Quaternion.AngleAxis(-1 * angleRotation, new Vector3(1.0f,0,0)) * result; // x = 1
    41.                 }
    42.                 else if (Input.GetKey(KeyCode.K))
    43.                 {
    44.                     result = Quaternion.AngleAxis(angleRotation, new Vector3(1.0f, 0, 0)) * result; // x = 1
    45.                 }
    46.             }
    47.  
    48.             if (Input.GetKey(KeyCode.J) && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.K))
    49.             {
    50.                 result = Vector3.up;
    51.             }
    52.  
    53.             if (Input.GetKey(KeyCode.K) && !Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.D) && !Input.GetKey(KeyCode.J))
    54.             {
    55.                 result = Vector3.down;
    56.             }
    57.         }
    58.  
    59.         this.transform.LookAt(result);
    60.  
    61.         return result;
    62.     }
    63.  
    64.     void Update()
    65.     {
    66.         var vectorMovement = GetMovementVector();
    67.  
    68.         transform.position = transform.position + vectorMovement * speed;
    69.     }
    70.  
    71. }