# Script to intelligently move aircraft

I am trying to code a script to move an aircraft entity from point A to point B and at some point in the middle of moving have the aircraft forward vector face Point C (to fire a missile at an enemy entity at C).

I implemented a bezier curve script that takes as input a list of waypoint and tangent handle Vector3 (Tangent handle is angle at point + weight which is represented by the length of the handle) which I plan to use to move the aircraft alone but I have not figured out a good algorithm to determine where to move to and the tangents at those points.

The control points in the img above are the same as my tangent handles.

The conditions are:
1 - Aircraft must start at A, waypoint 0 is therefore at A
2 - Tangent 0 angle must match Aircraft starting forward vector direction
3 - Waypoint N (last waypoint) must be at point B (finishing location).
4 - At some Waypoint (1 - N) the tangent must be facing the enemy position C
5 - The last tangent must be level with the y = 0 plane (aircraft must be parallel with the ground)

Constraints
1 - A,B,B are on the y=10 plane. This does not mean that waypoints in the middle of the movement need

The aim is to get smooth and somewhat natural looking movement, overly sharp changes of direction are undesirable.

My initial implementation was as follows:

``````        Vector3 middleWaypointPosition =
new Vector3((aircraftTransform.position.x + movementLocation.x + enemyTransform.position.x) / 3f,
(aircraftTransform.position.y + movementLocation.y + enemyTransform.position.y) / 3f,
(aircraftTransform.position.z + movementLocation.z + enemyTransform.position.z) / 3f);
Vector3 finishTangent = middleWaypointPosition + (enemyTransform.position - middleWaypointPosition) / 2;
finishTangent.y = 10;
// Add first point - A position, A forward tangent
waypointSequence.addPoint(aircraftTransform.position, aircraftTransform.position + aircraftTransform.forward * 10);
// Second point - Centroid of A, B, C. Tangent is pointing at C
// Last point - position is at B
(movementLocation - attackUnit.transform.position).normalized * 10);
``````

It works somewhat well in most cases but there are a number of edge cases, for instance A and B being at the same position makes the craft move towards the target then suddenly turn back with a 180 degrees turn towards B. There are a number of cases where I observed strange behavior.

Does anyone have any suggestions on how to design this algorithm?

Would a tween work? You could in theory set the tween when needed and run it. It would allow for a smooth benzier curve etc. you can adjust the time of the tween to be a factor of the distance between A and B.

The movement along the curve is already constant speed, I am more worried about how to design the algorithm that will be placing the waypoints in an intelligent fashion. The goal is to minimize sharp changes in angles.

natural movement is probably not going to be achieved by curves. I would create the flight controls and let the AI determine the path. There are a few factors you need the AI to understand. What is the approach vector distance for any given pattern. What is the speed of the craft, and does it change. Things like that. From those, you will then need to make a simple turning AI that does what you want. The waypoints would represent what you are thinking, but I would only have in and out. It is more natural if they fly after objects on their own.

So consider that your craft is going from A to B. B’s in vector is X, in order to achieve X, it has to have an approach of Y. (this is based on speed) So it’s actualy path is X. In order to achieve this vector it must then ask, if it can come close to the normal between B and X, if not, it will need to loop around to get a better angle. Always loop away from B.
Once the loop is done, it should then recalculate and attempt to land at B again. This introduces States. So you have a state of Looping and a state of Landing. next you introduce a state of Alert. This is a enemy ship that has come into the craft’s visual range. This state means that you always turn into the craft making hard banks as much as you can. Once you line up with the craft you shoot it down.

No more crafts, You go back to landing and restart.

That makes a lot of sense to design the flight movement rather than curves, hadn’t thought about that thanks.

The problem I am actually trying to solve is quite constraint, the movement is actually a just a visualization of an aircraft attack in a turn based strategy game. The moving plane always starts from a static position in the air attacks an enemy unit (which is static) and moves to the final location (in the air). The goal is to have something that looks plausible and hopefully be quite flashy as well :). I am guessing I would have 2 main logical states:

Start → [Attack enemy] → [Move to final location] → Finish.

The only thing I am slightly confused about is the looping behavior. The goal for the attack state would be to align with a given direction that is pointing at the enemy craft at a position on the direction where I still have the distance to pitch out of a collision. I am guessing solving the problem of getting aligned with this attack vector is going to be quite difficult. I had thoughts about situational canned movement paths in order to align using curves but is there a more dynamic way to calculate the movement?

That sounds much like what I had described minus the realtime aspect of it.

1. get the point of your target. Check the distance against the Dot of the plane’s forward. There is some math there that gives logic. If the dot is greater then the speed, turn towards the ship, if it is less, bank away. There is probably more logic for when the dot is negative as well.

2. once you are aligned and in firing position, fire

3. change the target back to the origin. Add the incoming vector in as well.