Search Unity

  1. We would like to hear your feedback about Unity and our products. Click here for more information.
    Dismiss Notice

Help With Drone Plane Landing Trajectory

Discussion in 'Physics' started by GreatWhite, Feb 3, 2015.

  1. GreatWhite


    Dec 13, 2012

    I’m making a game where I have drone planes (prefab instances) that fly to a selected point, bomb the target and then have to return to the base and land.

    With the code I have so far, when the drone is returning from a direction that almost aligns with the runway, everything works fine. I have some waypoints (GameObjects) around the strip as intermediate targets to keep the drones flying high until they are close to the base (otherwise they would start descending much earlier). The problem I’m having is when the drone approaches from a perpendicular or opposite direction: even with the waypoints, it dives and makes a sharp turn/barrel roll which, although highly acrobatic, is not how I want it to behave.

    I would like the drone to make a longer turn to smoothly align with the runway (see pic):

    I've been studying the code for several homing/seeking missile examples and I think that by limiting the rotation on the X axis (pitch) downwards, the drone would have circle around to reach the target as it overshoots and descends slowly on every turn in a spiral trajectory (not my preferred solution, but that would do better than the dive), but I have no idea how to do that...

    Here’s my code:
    Code (CSharp):
    2. GameObject Target, Runway, Hangar, Waypoint;
    3. Vector3 forceDir, forceFwd;
    4. float speed;
    5. bool landed;
    7. void Awake () {
    8.     speed = 10f; // Set speed to 10
    9.     forceDir =; // Initialize to zero
    10.     forceFwd =; // Initialize to zero
    11.     landed = false; // Hasn't landed
    12.     Waypoint = GameObject.Find ("Waypoint");
    13.     Runway = GameObject.Find ("Runway");
    14.     Hangar = GameObject.Find ("Hangar");
    15.     Target = Waypoint;
    16. }
    18. void FixedUpdate () {
    19.      // Vector to target:
    20.     forceDir = Target.transform.position - transform.position;
    21.     // Vector forward equals speed value:
    22.     forceFwd = rigidbody.transform.forward * speed;
    23.     // Final vector
    24.     Vector3 forceFinal = forceDir.normalized + forceFwd.normalized;
    26.     // If flying and approaching Runway...
    27.     if (!landed && Target == Runway) {
    28.         // Smoothly slow down to avoid overshooting:
    29.         speed = Mathf.Lerp (speed, 1.0f, Time.deltaTime * 6f);
    30.     else {
    31.         // If has landed and is headed to Hangar...
    32.         if (landed && Target == Hangar) {
    33.             // Smoothly brake towards Hangar
    34.             speed = Mathf.Lerp (speed, 0.2f, Vector3.Distance(transform.position, Target.transform.position);
    35.         }
    36.     }
    37.    // Add force to move the drone:
    38.    rigidbody.AddForce (forceFinal * Time.deltaTime * speed);
    39.    // Look towards where it's moving:
    40.    rigidbody.rotation  = Quaternion.LookRotation (forceFinal);
    41. }
    43. void OnTriggerEnter (Collider col) {
    44.     // If collided with Waypoint target trigger...
    45.     if ( == "Waypoint") {
    46.         // Set target to Runway
    47.         Target = Runway;
    48.     }
    50.     // If collided with Runway target trigger...
    51.     if ( == "Runway") {
    52.         // Mark as landed:
    53.         landed = true;
    54.         // Set target to Hangar:
    55.         Target = Hangar;
    56.     }
    58.     // If collided with Hangar target trigger...
    59.     if ( == "Hangar") {
    60.         // Destroy prefab instance:
    61.         Destroy(this);
    62.     }
    63. }

    Thanks in advance! any help would be much appreciated...
  2. andeeeee


    Jul 19, 2005
    Hi, welcome to the forum!

    With the setup shown in your picture, it looks as though you may just be slowing down too quickly. A good way to tweak the value to get it just right is to replace the constant 6f with a public variable. This variable will then be visible and editable in the inspector while the game runs and you can change it easily over multiple runs until you find the ideal value.

    However, it may still be quite tricky to get a nice landing trajectory purely by reducing the speed. You might want to consider having another target mode called "Descent" or something and use additional waypoints to define the line that the drone should take.
  3. GreatWhite


    Dec 13, 2012
    Hi Andeeeee,

    Thanks for your response, I thought there could be a simpler physics-based solution as it almost worked, but I'll just use more waypoints instead to guide the drone as you suggested.

    Thanks again!