- Home /
Unable to AddTorque to a HingeJoint with spring force,Hinge joint with spring ignores AddTorque
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: https://youtu.be/w1vxVBoZF6c
Is HingeJoint's spring force and AddTorque incompatible? If so, is there any way I can add proper torque to the joint?
[1]: https://youtu.be/w1vxVBoZF6c
Answer by llermy · Sep 17, 2021 at 07:38 AM
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.
Sorry for being rude, but why do you even ask a question if you don't bother to read the provided answers carefully? Both sides waste their time.
if your max angular velocity is low (default 20) than even very high torque won't change your rotation much,
Well, although you mentioned that part, the solution you have proposed does not solve the problem I state. When I was trying your solution and discussing it with you, you did not explain that Rigidbody.maxAngularVelocity
only caps AddTorque
and not the spring torque so increasing it would solve the problem. I'm sorry I have not been intelligent enough to try that before, but I think you downvoting this answer only makes people who have the same problem (which I have found in other threads without solution) harder to find a concise and real solution.
Answer by GamerLordMat · Aug 19, 2021 at 02:38 AM
see my video: https://youtu.be/4FZNab_dTWQ Hello, 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 https://youtu.be/4FZNab_dTWQ
The HingeJoint.spring
object you mention is actually the same as the spring option in the editor in HingeJoints. But I also checked to add the spring joint by script as you mention, so as to confirm whether it is a problem of the editor option, with:
HingeJoint joint = GetComponent<HingeJoint>();
JointSpring spring = joint.spring;
spring.spring = 60;
spring.damper = 1.5f;
joint.spring = spring;
joint.useSpring = true;
But it does not change anything. By the way, you need to set HingeJoint.useSpring
to true if you want to add spring by script.
The problem is not with the damper. If the spring value is 0, the torque is added correctly with any damper value, it just rotates slower as expected due to the damping force. But if I set the spring value, the torque doesn't make any significant rotation even with very high torque values as parameter of`AddTorque` function.
see my video https://youtu.be/4FZNab_dTWQ
like I said... it tries to reach the goal angle 0, so any force you apply will be counteracted. Here my project where I reacreated a PID controller with machine learning, and you clearly see how it tries get to 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 $$anonymous$$imize the error between the specified value and the current value, and I highly suspect that spring is implemented this way.
Either use spring (defining an angle) or torque, not both.
Thanks for the video but it's private so I cannot see it :) I'm actually not talking about a torque of 30, even a torque of 100000 does not rotate the joint noticeably. However, I suppose you mean that HingeJoint.spring
and AddTorque
are not compatible and there is no solution.
Your answer

Follow this Question
Related Questions
How to rotate rigidbody relative to jointed other rigidbody? 2 Answers
Adding Torque to a Hinge Joint 1 Answer
Is there a 2d joint for a torsion (rotation) spring? 0 Answers
Physic Torque Hinge joint problem 0 Answers
how connect 2 object 1 Answer