Limit transform.LookAt to 90 degrees based off of object's forward axis

Please do not refer me other scripts or asset store packages, mecanim or otherwise, thank you.

I am now at a point in my game where I would like to have my characters heads look directly at other points.

I have a lot of characters rigged with bones not ideally oriented to have correct rotations, ie: Head /neck bones with the y axis backwards or flipped, or something weird. When I first started modelling, I was not aware of the importance of setting all bones axis’ orientations.

Anyways, It’s simply not ideal for me to go and re bone, and re skin, and re animate everyone from scratch.

I have accomplished the rotation fix by using space self and transform.Look At

var pref :  Vector3; 
var point : Transform;


function LateUpdate () {

 transform.LookAt(point.position);
transform.Rotate(pref.x, pref.y, pref.z, Space.Self);
}

This works beautifully, but of course is unnatural for a head/neck bone. Imagine when the point an npc is looking at walks behinds them. The head will rotate too much.

This is what I need help with. How can I

Limit the rotation of a bone to not exceed a certain degree beyond the root character’s forward axis?

for example, when the point the npc is looking at goes over 90 degrees relative of the character’s forward axis (not the head/neck bone, because it’s axis is weird to start with ), make the head/neck bone STOP where it’s at.

I’m simply trying to not allow full 360 head rotation based of the forward of the initial npc’s object.

Use Vector3.Angle()to calculate the angle between your character’s forward vector and the point you’re looking at. If the angle is greater than 90 degrees, only rotate your head by 90 degrees, otherwise rotate it to the appropriate angle.