I am attempting to make an airplane automatically fly around a series of points in space. Right now the movement is locked to a plane along the X/Z axis (Y never changes) but ideally I would like to keep the option for full 3D flight open. The airplane constantly moves forward, while rotating around the Y axis to face its target, causing a little bit of an arc as it moves towards its target.
So far it is working well, but I am not sure how to add banking to the rotation calculations. As the plane makes sharp turns I want the plane to rotate around its forward axis.
I am just using the built in Quaternion.LookRotation function. I’ve tried setting the Z-rotation directly after the rotation towards the target is complete, but it seems to cause problems with the movement towards the target, and sends the plan off in undesired directions.
// Update is called once per frame
void Update ()
{
// Get the current target.
Transform currentTarget = targets[currentTargetIndex];
// Get a vector from the current position to the target.
// This is the vector we want to move our forward vector towards.
Vector3 lookDirection = currentTarget.position - transform.position;
// Calculate the rotation needed for this target look vector.
Quaternion rot = Quaternion.LookRotation(lookDirection.normalized);
// Rotate towards that target rotation based on the turn speed.
transform.rotation = Quaternion.Slerp(transform.rotation, rot, Time.deltaTime * turnSpeed);
// With the rotation complete, just move forward.
transform.Translate(new Vector3(0,0,moveSpeed) * Time.deltaTime);
}
Let me know if there is anymore information I can add.