If you post a code snippet, ALWAYS USE CODE TAGS:
How to use code tags: Using code tags properly
Camera stuff is pretty tricky… it might be best to use Cinemachine from the Unity Package Manager.
There are tons of tutorials on how to set up either a basic camera or a Cinemachine camera to do precisely what you seek. Go start with Youtube.