How rotate camera that follows object according to object y axis?

Hi,

I see several scripts out there but none seem to work!
I want to rotate my camera that follows an object according to that object y axis? like a follow behind script? Has anyone got any tips or samples available? I can’t get anything to do this (smooth follow does not seem to work).

Thanks

Something like this:

public Transform transformToFollow;
 
void Update() {
     transform.position = new Vector3(transform.position.x, transformToFollow.position.y, transform.position.z);
     transform.rotation = transformToFollow.rotation;
}

Note how the transform.position Vector uses the camera’s own x any z values, and only the transformToFollow’s y value.

If the camera should have a fixed x and z value, you can also do it the other way around: Make the camera a child of the object that it should follow, and then reset the transform.position’s x and z values in Update():

public Transform transformToFollow;
public float pos_x;
public float pos_z;
    
    void Update() {
         transform.position = new Vector3(pos_x, transform.position.y, pos_z);
    }