Rigging: Limiting motion on one axis to when the object is in a specific position on another
In my setup there are 3 bones, one controls the body, one controls the head and one marks the center loc of the head. Center and head are child of the body. Head bone has been rotated on x axis -90 degrees, to match world orientations, and has z loc locked.
Head bone has two limit loc constraints, one for X axis, one for Y axis.
Their influence is driven by two similar drivers.
The first driver has two variables, transform channel kind, evaluating Y loc of the head bone and Y loc of center bone.
I've named the variables with the same name of the bones, but they can have any name you want.
The expression says: absolute value of difference between Y loc of head and Y loc of center is &gt; 0.1 (you can vary this value).
If the condition is true 1.0 is the output, if false 0.0.
The second driver is the same but controls limit Y loc, evaluating X loc.
Every constraint and driver has to be set in local space, in order to work even when the character is rotated, translated and scaled.
In the first limit loc constraint you can add a limit y loc from -1 to 1 (and the same in the second for X loc), in order to prevent the head going outside the body boundaries.