INCOMING!!
Camera stuff is pretty tricky… you may wish to consider using Cinemachine from the Unity Package Manager.
There’s even a dedicated forum: Unity Engine - Unity Discussions
But check this out: I got a new blurb I’m working on: how to make your own camera controller:
If you insist on making your own camera controller, the simplest way to do it is to think in terms of two Vector3 points in space: where the camera is LOCATED and where the camera is LOOKING.
private Vector3 WhereMyCameraIsLocated;
private Vector3 WhatMyCameraIsLookingAt;
void LateUpdate()
{
cam.transform.position = WhereMyCameraIsLocated;
cam.transform.LookAt( WhatMyCameraIsLookingAt);
}
Then you just need to update the above two points based on your GameObjects, no need to fiddle with rotations.
Finally:
Notes on clamping camera rotation and NOT using .eulerAngles because of gimbal lock:
How to instantly see gimbal lock for yourself:
All about Euler angles and rotations, by StarManta:
https://starmanta.gitbooks.io/unitytipsredux/content/second-question.html