The vision of a camera (displayed as a 3D Cone) needs to rotate towards the player

The script needs to rotate the gameObject when the Player enters its collision, so that the player is always in the center of the x and z. The camera doesn’t move, it can only rotate. When the player leaves the cone, it returns to its original position. The origin of the camera is the top of the cone. Here’s my code so far:

private void OnTriggerStay(Collider other)
    {
        if (other.gameObject.tag == "Ghost")
        {
            float x = other.gameObject.transform.position.x - transform.position.x;
            float z = other.gameObject.transform.position.z - transform.position.z;

            // Set x and z to positives if they aren't already
            if (x < 0)
            {
                x *= -1;
            }

            if (z < 0)
            {
                z *= -1;
            }

            transform.parent.gameObject.transform.Rotate(0, CalculateRotation(x, z), 0);
            // transform.parent.gameObject.transform.rotation = new Quaternion(0, 0, 0, 0);
        }
    }

private float CalculateRotation(float x, float z)
    {
        if (z <= 0)
        {
            return 0f;
        }

        float degrees = 0f;
        float total = x + z;

        float part = z / total;
        degrees = part * -90f;

        print("X: " + x);
        print("Z: " + z);
        print("Total: " + total);
        print("Degrees: " + degrees * -1);

        return degrees;
    }

The problem here is that transform.Rotate() keeps triggering, always rotating the camera too far. transform.rotation = new Quaternion() rotates wrongly however, and I can’t figure out the pattern of numbers (new Quaternion(90, 0, 90, 0) results in a rotation of (180, 90, 0) for the object, for example).

Does someone have experience / a script regarding the topic? I’ve already searched online but I’m not sure how to describe the issue, and as such have a hard time finding solutions.

i have some questions, why are you trying to reimplementate the Unity - Scripting API: Transform.LookAt yourself? any special needs? and if its because you need to rotate it step by step, cant you use Unity - Scripting API: Vector3.RotateTowards

for example

void Update()
{
   Vector3 target = other.transform.position - transform.position;

   float step = speed * Time.deltaTime;

   Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0f);

    transform.rotation = Quaternion.LookRotation(newDir);
}