IK Hinge

i need to make a hinge with two bones in an L shape that will be pulled by an IK. this has to be in 3D, since later itll need to work on odd angles and as part of a bigger IK chain.

i cant seem to figure out how to limit it to rotate straight around a specific axis. somehow the ik always ends up pulling it away and or off axis

untitled12.blend (561.6 KB)