# Help With Drone Plane Landing Trajectory

Hi,

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:

``````GameObject Target, Runway, Hangar, Waypoint;
Vector3 forceDir, forceFwd;
float speed;
bool landed;

void Awake () {
speed = 10f; // Set speed to 10
forceDir = Vector3.zero; // Initialize to zero
forceFwd = Vector3.zero; // Initialize to zero
landed = false; // Hasn't landed
Waypoint = GameObject.Find ("Waypoint");
Runway = GameObject.Find ("Runway");
Hangar = GameObject.Find ("Hangar");
Target = Waypoint;
}

void FixedUpdate () {
// Vector to target:
forceDir = Target.transform.position - transform.position;
// Vector forward equals speed value:
forceFwd = rigidbody.transform.forward * speed;
// Final vector
Vector3 forceFinal = forceDir.normalized + forceFwd.normalized;

// If flying and approaching Runway...
if (!landed && Target == Runway) {
// Smoothly slow down to avoid overshooting:
speed = Mathf.Lerp (speed, 1.0f, Time.deltaTime * 6f);
else {
// If has landed and is headed to Hangar...
if (landed && Target == Hangar) {
// Smoothly brake towards Hangar
speed = Mathf.Lerp (speed, 0.2f, Vector3.Distance(transform.position, Target.transform.position);
}
}
// Add force to move the drone:
rigidbody.AddForce (forceFinal * Time.deltaTime * speed);
// Look towards where it's moving:
rigidbody.rotation  = Quaternion.LookRotation (forceFinal);
}

void OnTriggerEnter (Collider col) {
// If collided with Waypoint target trigger...
if (col.name == "Waypoint") {
// Set target to Runway
Target = Runway;
}

// If collided with Runway target trigger...
if (col.name == "Runway") {
// Mark as landed:
landed = true;
// Set target to Hangar:
Target = Hangar;
}

// If collided with Hangar target trigger...
if (col.name == "Hangar") {
// Destroy prefab instance:
Destroy(this);
}
}
``````

Thanks in advance! any help would be much appreciated…

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.

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!