Hi! I’m facing a very interesting problem. I have three connected hinge joints joined together to form the leg of a character. These hinge joints use the hinge joint spring option with spring parameter in the range of 50-240 and damping paramenter in the range of 1.5-6. All these hinge joints can only rotate around de z axis.
And this works as expected. The problem is, when the spring option is activated I am not able to add any significant torque around the axis in FixedUpdate with a simple:
GetComponent<Rigidbody>().AddTorque(axis * torque);
It does not matter the magnitude of the torque, it only rotates the hinge joint a tiny bit. If I debug the value of
GetComponent<HingeJoint>().currentTorque, I can see the torque is really very low. If I deactive the spring option of the HingeJoint, the torque is added as expected. If I have a lower spring parameter, AddTorque seems to add more torque, but the HingeJoint is not as stiff as I require.
I’ve uploaded a video so you can check the problem with a minimal example: Unity Hingejoint with spring unable to properly add torque - YouTube
Is HingeJoint’s spring force and AddTorque incompatible? If so, is there any way I can add proper torque to the joint?
see my video: HInge Joint Test - YouTube
you don’t usually do that with a spring activated, bc it tries to counteract all forces (plus damping slows the motion down).
A spring tries to reach the goal angle (default is zero), so any force/torque you apply will be counteracted if it moves the angle away from the target destination.
In most cases you want to use spring (defining an angle) or torque, not both (but it is compatible).
currentTorque is the torque it uses to satisfy all constraints.If your max angular velocity is low (default 20) than even very high torque won’t change your rotation much, thus the currentTorque to counteract it won’t be high.
try to use instead:
HingeJoint joint = GetComponent<HingeJoint>();
JointSpring spring = joint.spring;
spring.spring = 60;
spring.damper = 1.5f;
joint.spring = spring;
joint.useSpring = true;
and define the angle, so it will try to reach this angle automatically.
Here my project where I created a PID-like controller with machine learning, and you clearly see how the spring tries to get to angle 0, even though my Machine Learning Agent tries to achieve another angle and applies torque, but has only 30 Torque, so the spring “wins”. A PID Controller tries to minimize the error between the specified value and the current value and I think that spring is implemented in a similar fashion.
see my video HInge Joint Test - YouTube
It seems the solution is actually quite simple. It is due to that only
AddTorque is limited by
Rigidbody.maxAngularVelocity, but not the resulting torque including the spring torque, so the applied torque could not overpower the spring. So making
Rigidbody.maxAngularVelocity bigger solved the problem.