How do you rotate the camera while it's attached to an object?

I found some c# code that lets you do this, but it’s very very glitchy. Doesn’t have any center, and always faces one direction even when the object the camera is attached to rotates.

using UnityEngine;
using System.Collections;

public class Camera : MonoBehaviour {
    public float speedH = 3.0f;
    public float speedV = 2.0f;
   
    private float yaw = 0.0f;
    private float pitch = 0.0f;
   
    void Update () {
        yaw += speedH * Input.GetAxis("Mouse X");
        pitch -= speedV * Input.GetAxis("Mouse Y");
       
        transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
    }
}

How would I make a camera that always faces the direction the object is facing, and also have it rotate with the mouse so it can look left and right, without the camera angle glitching, being weird and not having anything centered at all?

It’s not “glitchy,” it just doesn’t do what you want. I’m not clear on what “doesn’t have any center” means, but for your second complaint, I think you could solve that by just assigning to localEulerAngles instead of eulerAngles.

You can’t. Right off the bat you have listed two contradictory requirements.

…and followed that up with additional requirements so vague as to be meaningless.

I think you’ll find that most of programming is being able to clearly define the behavior you want. Once you can do that, writing it in code isn’t that hard. But sometimes thinking clearly about what you want isn’t as easy as it sounds!