- Home /
Hingejoint not responding on script
I have a hingejoint setup. When I press a key I can see the number at the "target position" in the inspector change but the mesh it is attached to does not move. But when I type in a random number at "target position" the mesh does move.
The script attached is very basic:
public float defaultPosition = 0.0f;
public float targetPosition = 50.0f;
public float springStrength = 100.0f;
public float springDamper = 1.0f;
private JointSpring spring;
void Awake ()
{
GetComponent<HingeJoint>().useSpring = true;
spring = new JointSpring();
spring.spring = springStrength;
spring.damper = springDamper;
}
void Update ()
{
if (Input.GetKeyDown (KeyCode.D))
{
Debug.Log ("D pressed");
spring.targetPosition = targetPosition;
}
else
spring.targetPosition = defaultPosition;
GetComponent<HingeJoint>().spring = spring;
}
-edit- The problem is stranger then I thought. I removed the script component, added it again end it worked. But once I change a public variable in the inspector it breaks. Besides that if I re-attach the script component it does not always work, sometimes I have to re-attach it several times.
I made some two video's: https://www.youtube.com/watch?v=hIPkPyYqAcY and https://www.youtube.com/watch?v=D1bH0Dkl7p4 showing the issue. I basically have to set the default position to -10 for it to work. But als a higher force or damper break things. It even came to a point that when I clicked out of the game window and back it broke.
Here is the project: http://forum.unity3d.com/attachments/pinball-zip.130468/
Basic, simple but not working...
I filed a bug report and made a github repo illustrating it...
https://github.com/stephancom/unity-hinge-bug
but as I play with it, I'm wondering if this is really a bug, or just a misunderstanding of how it works? Because in my sample project, it seems like it works but the hinge needs "a little poke" to get moving.
Answer by HarshadK · Apr 07, 2015 at 11:03 AM
Instead of creating a new Spring instance here, you need to get the current spring from the HingeJoint. Something like:
void Awake ()
{
GetComponent<HingeJoint>().useSpring = true;
// Get the spring from the HingeJoing
spring = GetComponent<HingeJoint>().spring;
spring.spring = springStrength;
spring.damper = springDamper;
}
Nope still the same result: nothing when I use the "D" key even with get$$anonymous$$ey
ins$$anonymous$$d of get$$anonymous$$eyDown
. And the damn thing is moving when I change the value manually in the inspector while running.
Answer by stephancom · Apr 09, 2015 at 04:31 AM
This appears to be a bug, I've created a github repository that illustrates the issue
https://github.com/stephancom/unity-hinge-bug
A very simple scene, when you walk into the collider, the spring should open the door, and it does not - the values change in the project, but there is no movement. Manually changing the values while it is running works.
How do I file a bug report, and do they pay bounties?
Answer by kinggryan · May 12, 2015 at 09:48 AM
I've also encountered this and devised a workaround. I'm not sure, but it seems like changing the constraints of a joint do not cause the physics engine to calculate a solution for the scene. By "prodding" the object whose joint's constraints have been changed, you can force a solution. For me, after changing the constraints of a joint, I called AddForce(Vector3.zero) on the object's rigidbody. This caused the new constraints to be enforced.
Answer by MarshCZA · Nov 06, 2018 at 02:00 PM
I've had the same problem and fixed it by calling
GetComponent().WakeUp();
After you change the spring values.
Your answer
Follow this Question
Related Questions
Set a HingeJoint's Target Position to point toward an object? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers