Hinges not working properly

I have a situation where some hinges are supposed to pivot along the z axis, and they use a spring to move the object. However, I am getting a twisting motion that allows outside forces like gravity to force the joint to twist along other axis as well. This is defeating the whole purpose of what I an trying to do. I am attempting a temporary solution. The application is to make a six legged insect walk using physics. This is working except that the legs twist along the undesired axis under the weight of the insect, and especially ass the insect increases in sped and the forces on the joints increase. I am already usiang and insanely strong spring, and that doesn’t help at all.

Is there a way to truly lock the unwanted axis?

Any takers? I have been thinking about this, and it seems that what I need is to make sure the feet remain perpendicular to the surface they are moving on. I have trying using gyroscope scripts to keep them upright, which works on surfaces that are perfectly level, but on uneven surfaces, where legs have an advantage, they get stuck and fail to work properly because they are forcing the entire insect to remain level on an un-level surface.