Changing sides with a fight game camera

I’ve been trying to figure this out for a few days and I’m not having any luck with it. I’m trying to create a camera similar to the one used in fighting games like Tekken or Soul Calibur. I’ve gotten it to mostly work, however there are a few glitches that keep popping up that I can’t seem to solve. Here is my code:

(Note - first and second target are the actual characters, while first and second camPointer are the objects that are used for the purposes of determining camera behavior. The camPointers are assigned to follow the movements of the characters.)

public class BattleCamera : MonoBehaviour
{
    public Transform firstTarget = null;
    public Transform secondTarget = null;
	public Transform firstCamPointer = null;
	public Transform secondCamPointer = null;
    public float cameraRotationSpeed = 20.0F;
	public float cameraMoveSpeed = 20.0F;
     
	private Vector3 midpoint = Vector3.zero;
	private Vector3 camSide = Vector3.zero;
     
    void Update()
    {
		firstCamPointer.transform.LookAt(secondCamPointer);
		secondCamPointer.transform.LookAt(firstCamPointer);
		
		float firstTargetDot = Vector3.Dot(firstCamPointer.position - camera.transform.position, camera.transform.right);
		float secondTargetDot = Vector3.Dot(secondCamPointer.position - camera.transform.position, camera.transform.right);
		midpoint = 0.5F * (secondCamPointer.position + firstCamPointer.position);
		
		if(firstTargetDot < 0)
			camSide = firstCamPointer.right;
		else if(secondTargetDot < 0)
			camSide = -firstCamPointer.right;
    }
     
    void LateUpdate()
    {
		firstCamPointer.position = firstTarget.position;
		secondCamPointer.position = secondTarget.position;
		
		camera.transform.rotation = Quaternion.Slerp(camera.transform.rotation, Quaternion.LookRotation(midpoint - camera.transform.position), Time.deltaTime * cameraRotationSpeed);
		camera.transform.position = Vector3.Lerp (camera.transform.position, midpoint + camSide * 10F, Time.deltaTime * cameraMoveSpeed);
    }
}

My problem is that the camera will occasionally glitch out for a split second when one character changes sides with another. It seems as if it’s trying to quickly swing itself over to the other side just as the transition is happening. This is only a momentary hitch; the camera will just as quickly revert back to where it is supposed to be after another frame or two. The effect is a sudden jolt of the camera whenever a player jumps over or slides underneath another, and it’s really distracting.

Even worse, sometimes the camera will swing to the other side and actually stay there. This gives the impression that a character, which has just changed sides, is still on the same side that they were previously on. This problem becomes worse the higher the cameraMovementSpeed value is raised.

Does anybody know what could be causing this behavior?

A few ideas to check out:

  • Your Slerp() code is not ‘slerping.’ That is, the last parameter is fairly constant instead of incrementing from 0 to 1. It ‘works’ because camera.transform.rotation is being recalculated each each frame and Slerp() increments a bit each frame. Consider using Vector3.RotateTowards() instead.
  • There is the same issue with your Lerp not ‘Lerping.’ Consider using Vector3.MoveTowards instead.
  • I now from experimenting that Slerp() takes the shortest path between two points, but if the points are around 180 degrees apart, small changes can make the path change wildly.