0
\$\begingroup\$

I'm facing serious problem with handling hinge joints dynamically in Unity.

I made bunch of chains and they are all linked for make something like "sealed door".

When I remove HingeJoint component at the runtime from Inspector, chains were unlinked and everything works fine.

But when I try same thing in the script, sometimes it works and sometimes not(And most of time doesn't work). And some condition just doesn't work.

There are two chain links so I need to remove two hinge joints, one named chain1 and one named chain2.

This is the code what am I tried:

Destroy(chain1.GetComponent<HingeJoint>());
Destroy(chain2.GetComponent<HingeJoint>());

Above code is just remove hinge joints but it doesn't worked always. So I added some delays:

IEnumerator RemoveHingeAfterTime(float delay, Rigidbody chainRB) {
    yield return new WaitForSeconds(delay);
    Destroy(chainRB.GetComponent<HingeJoint>());
}

...

StartCoroutine(RemoveHingeAfterTime(0.1f, chain1.GetComponent<Rigidbody>()));
StartCoroutine(RemoveHingeAfterTime(0.2f, chain2.GetComponent<Rigidbody>()));

Above code is just quite random, sometimes work sometimes not. Especially chain2 almost not works.

I adjusted time to fix this, but nothing works. "yield return null" or "yield return new WaitForFixedUpdate" also doesn't worked.

My doesn't worked means like this: enter image description here

enter image description here

enter image description here

Also I toggled isKinematics and useGravity with same logic(just hope that physics works) but doesn't worked either.

I'm using Unity 5.6.1f1. Is this glitch? Is there a way to fix this?

Any advice will very appreciate it.

\$\endgroup\$
10
  • \$\begingroup\$ I've never seen a component survive a Destroy call. Can you describe the symptoms you're seeing in a bit more detail? Is the component still present in the Inspector after it should have been destroyed, or is there just some leftover physics effect even though the component is gone? Have you verified you're destroying the hinge off the right GameObject, that it only had one HingeJoint to begin with, and that no other script is re-creating it? \$\endgroup\$
    – DMGregory
    Oct 6, 2017 at 11:51
  • \$\begingroup\$ @DMGregory Component was destroyed but physics not working properly only when I remove from script. \$\endgroup\$
    – modernator
    Oct 6, 2017 at 13:44
  • \$\begingroup\$ @DMGregory First picture shows the target that I want to rid of hinge joint component. Second picture shows that remove hinge joint from script doesn't work properly. Third picture shows that remove hinge joint form inspector works fine. \$\endgroup\$
    – modernator
    Oct 7, 2017 at 2:50
  • \$\begingroup\$ Information required to understand your problem should be added to your question itself as an edit, not hide in the comments. ;) So, we could describe the problem.as "links of the chain upstream from where the joint was removed don't react after it's gone, but links downstream swing away as expected"? Sounds like the physics bodies might just be asleep and need a nudge to wake them. \$\endgroup\$
    – DMGregory
    Oct 7, 2017 at 2:54

1 Answer 1

1
\$\begingroup\$

I solved this by give the little force and forcely wake their rigidbody to every chain over there when it unlinked!

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .