Make my camera perpendicular to two objects

I’m trying to move my camera (A) in the middle and perpendicular to two objects (at C and D). I can find the midpoint in (B), but I can’t get my mind around the math that is needed to rotate my camera to be facing the mid point (B) too.

alt text

Moving it to the middle is no problem, I can use this:

    public static Vector3 GetMidPointBetweenTwoObjects(Vector3 sourceObject, Vector3 targetObject)
        Vector3 result = new Vector3(0, 0, 0);
        result.x = sourceObject.x + ((targetObject.x - sourceObject.x) / 2);
        result.y = sourceObject.y + ((targetObject.y - sourceObject.y) / 2);
        result.z = sourceObject.z + ((targetObject.z - sourceObject.z) / 2);
        return result;

But how can I find the rotation for my camera at A, that is perpendicular to these two objects, for point B?

To calculate the location in the middle, you can use Vector mathematics instead of doing it manually:

Vector3 result = sourceObject + (targetObject - sourceObject) / 2;

If you want to move it along the line towards A, simply rotate the vector between D and C left by 90 degrees, then add it to B.

Vector3 midCD = sourceObject + (targetObject - sourceObject) / 2;
Quaternion ninetyLeft = Quaternion.AngleAxis(90, Vector3.up); // to rotate 90 degrees left, rotate 90 degrees around the Y axis
Vector3 towardsA = (ninetyLeft * (targetObject - sourceObject)).normalized; // rotate the vector that points from C to D by 90 degrees left, then you get the vector that points from B to A. Normalize to make it of size 1.
Vector3 result = midCD + towardsA * distanceTowardsA;

Then to put the camera and make it face point B, you can use transform.position and transform.LookAt()`:

transform.position = result;

If you have B, and your camera is at A, then use transform.LookAt(B); in a script on the camera object.