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:
- Aircraft must start at A, waypoint 0 is therefore at A
- Tangent 0 angle must match Aircraft starting forward vector direction
- Waypoint N (last waypoint) must be at point B (finishing location).
- At some Waypoint (1 - N) the tangent must be facing the enemy position C
- The last tangent must be level with the y = 0 plane (aircraft must be parallel with the ground)
Constraints
- 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:
waypointSequence.addPoint(position, tangentHandlePosition)
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
waypointSequence.addPoint(middleWaypointPosition, finishTangent);
// Last point - position is at B
waypointSequence.addPoint(movementLocation, movementLocation +
(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?