What I do when I want the camera to follow something is to keep setting the position of a hidden plane to the x, y and z of the object you want to follow, then position the camera relative to that object.
See the attached code.
In the code below, CAMTARGET is the hidden plane which follows the object. I can change which object is being followed by changing the value of FOCUS to the number of the object being followed. CAMDIST is the distance between the camera and the object being followed.
You could perhaps add a bit to make CAMTARGET point in the same direction as the object being followed, as the code attached won't do that for you (I have that part in a separate function, controlled by user input).