Clamping head rotation relative to parent

FINAL EDIT: Please read all replies of the accepted post. Here’s the final code:

private void ControlHead(Transform target)
{
    Vector3 targetPosition = new Vector3(target.position.x, 0, target.position.z);
    Vector3 headPosition = new Vector3(myHead.position.x, 0, myHead.position.z);
    Vector3 relativePos = targetPosition - headPosition;
    float angle = Vector3.SignedAngle(transform.forward, relativePos, Vector3.up);
    float clampedAngle = Mathf.Clamp(angle, -90, 90);
    myHead.localRotation = Quaternion.Euler(0, clampedAngle, 0);
}

How would I stop my NPC’s head from looking behind when looking at a target? It seems the clamping is only happening in world space so no matter what direction the parent body is facing, the head will clamp at the same position.

The code:
**
private void ControlHead(Transform target)
{
Vector3 relativePos = target.position - myHead.position;
float angle = Quaternion.LookRotation(relativePos).eulerAngles.y;
float clampedAngle = Mathf.Clamp(angle, -90, 90);
myHead.localRotation = Quaternion.Euler(0, clampedAngle, 0);
}
**
EDIT: please look at the replies to Captain_Pineapple’s answer.
EDIT: video of the issue: https://youtu.be/Tdxkq8g7jKQ

Hey there and welcome to the forum,

this topic should really be one of the main unity tutorials that unity forces you to take when starting.

First problem:
An euler angle representation of a rotation in 3D space is not unique. That means for one rotation there are always multiple representations in euler angels. So you cannot rely on this.

Second and more important problem:
If you Debug.Log your Quaternion.LookRotation(relativePos).eulerAngles.y you will see that it will never return a negative value but instead it will wrap around to go down from 360.
So in this case you basically at least have to add in another step like this:

  if(angle > 180)
          angle -=360;
  float clampedAngle = Mathf.Clamp(angle, -90, 90);

This should do the trick. Assuming that your Quaternion.LookRotation(relativePos) rotation is only around the y-axis you might be lucky and this will work. If it is a mixed rotation of any form where x and z are not zero this will fail.