Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by MaGuSware™ · Dec 23, 2012 at 10:56 AM · c#setactivehingejoint

HingeJoint stops working properly after disable/enable.

Greetings,

It is as the title says, the hinge joints stop working properly after they have been disabled and enabled. The best I can describe it is the limits are setting themselves to something other than what I have defined. However, when watching them in the inspector this does not appear to be the case.

The "DoorSwing" is being activated in my game pause script, using the following code:

 void SetPause(bool pause)
 {
     state = 0;
     isPaused = pause;  
     Screen.showCursor = pause; 
     
     foreach (GameObject go in objects)
     {
         if (go != gameObject && go != null)
             go.SetActive(!pause);
     }
 }

Here is a video of the problem. - Warning: I left my music on, might be loud.

I don't think this is the problem is with the hinge door class, however, I could be wrong... here is the hinge door script.

This problem has been rattling my brain for a few days and no matter how much I think about it I cannot solve it. So time to turn to the community.

Any thoughts guys?


Peace,

MaGuSware™

Comment
Add comment · Show 21
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MaGuSware™ · Dec 23, 2012 at 11:04 AM 0
Share

That's gameObject, as in the scripts own reference to the gameObject it is in. Just making sure it doesn't disable itself.

avatar image Fattie · Dec 23, 2012 at 11:13 AM 0
Share

ah sorry, sure. Hmm ... regarding your problem.

I've found with hinges if you DISABLED them and then $$anonymous$$OVE the objects involved, it causes problems when you re-enable them.

I had a situation with a parent-child chain A-B-C-D. I think C or something had a hinge. As I say, if I disabled the hinge, and then happened to move the whole thing (A) say offscreen or whatever - when I reenabled, trouble.

In that case, the solution was "disable everything fropm the highest level sdownwards" (so from A downwards, setActiveRecursively in the Old Unity) - and only then move anything. So that was the solution.

Perhaps this is relevant to your situation here -- is something (some part of the hinge. or something relating to some part of the hinge) being moved while it is disabled?

In a word you wouldn't be able to "manually" move open/close etc the door, while the hinge is asleep.

In a similar vein: when moving and/or "restarting" hinges, I find you have to zero all the physics on all rigidbodys involved. You can't have the hinge "co$$anonymous$$g in to" any PhysX momentum (linear or angular)

Hope it 's relevant !

avatar image MaGuSware™ · Dec 23, 2012 at 11:39 AM 0
Share

Hmm, that is insightful, unfortunately, I'm not sure about how to zero to physics on a rigid body.

I tried setting it to kinematic with OnDisable and back to dynamic with OnEnable. I also tried making sure the motor is not used and that the rigid body is forced to sleep.

None of that worked.

avatar image Fattie · Dec 23, 2012 at 11:44 AM 0
Share

to "zero" physics, easy as pie -- essentially, you can use the .Sleep() command, or, you can zero the velocity and angularVelocity.

You'd have to do this up and down the chain for every rigidbody involved in any way in the hinge. (To repeat myself, hinges are way fussy about physics, it is very hard to "restart" them - as you've found.)

Here is an extended discussion between myself and @Bunny83 (local boy genius) about zeroing physics in Unity.

http://answers.unity3d.com/questions/269964/zero-ing-out-physics-in-unity.html

avatar image Fattie · Dec 23, 2012 at 02:44 PM 1
Share

For sure - just as you say it's completely ridiculous to use timeScale=0 in a real-life project, but I just mention it for completeness for any one reading through in the futchya.

It\'s clearly something where hopefully in the future Unity engineer it inside from the ground up. Let's hope they do.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
9

Answer by standardcombo · Apr 09, 2014 at 06:48 AM

Add the following component to each hinge joint.

 using UnityEngine;
 
 public class HingeJointFix : MonoBehaviour
 {
     private Quaternion initialLocalRotation;
     private Vector3 initialLocalPosition;
 
     private Quaternion localRotationOnDisable;
     private Vector3 localPositionOnDisable;
 
     private bool hasDisabled;
 
     void Awake()
     {
         this.initialLocalRotation = this.transform.localRotation;
         this.initialLocalPosition = this.transform.localPosition;
     }
 
     void OnDisable()
     {
         this.localRotationOnDisable = this.transform.localRotation;
         this.transform.localRotation = this.initialLocalRotation;
 
         this.localPositionOnDisable = this.transform.localPosition;
         this.transform.localPosition = this.initialLocalPosition;
 
         this.hasDisabled = true;
     }
 
     void Update()
     {
         if (this.hasDisabled)
         {
             this.hasDisabled = false;
             this.transform.localRotation = this.localRotationOnDisable;
             this.transform.localPosition = this.localPositionOnDisable;
         }
     }
 }


It seems the Hinge class recalculates the limits each time it gets OnEnable. This code patches it by reverting the transform to the same position/rotation from startup. Then the limit calculation at OnEnable will be consistent. Finally, in the next Update it restores the transform to what its supposed to be.

You can see this in action in my Perseus 230 demo: http://perseus230.com/ship_viewer_guardian.html (When you switch ships back and forth the Guardian Drone is enabled/disabled, hinges in the arms are ok!)

In case the base rigid body to which the last hinge is jointed moves/rotates, add the component to it as well. If you don't desire this entire behavior to be on the base object--lets say the base rigid body is the wall in a level and the hinge is a door; and for some reason the walls move!--create a rigid body that is a child of the wall and add this component, then hinge the door to it instead of to the wall.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image wildbean · Apr 24, 2017 at 10:08 AM 0
Share

Thank you for this, really helped me out!

avatar image SuppleTeets · Feb 23, 2018 at 11:42 PM 0
Share

This was killing me, thank you! I was resetting OnEnable, but it's the OnDisable reset that does the trick!

avatar image henrrygesposito · Apr 10, 2020 at 10:43 PM 0
Share

Works for me too, thanks!

avatar image zacharyaghaizu · Feb 17 at 07:29 AM 0
Share

Thank you so much!! helping us out

avatar image
0

Answer by True_Beef · May 08 at 05:31 PM

So if the above solutions didn't work for anyone, this one might. After battling with this Issue for a day or so, I've found a method that should work no matter the setup of your parents and objects. The concept is to take the rotation of the joint-object at initialization and transform that quaternion to be relative to the connected body's coordinate plane. You then cache it as the "Initial Rotation".

 Private Quaternion initialRotation_JointToConnected
 initialRotation_JointToConnected = Quaternion.Inverse(joint.transform.rotation) * joint.connectedBody.transform.rotation; //Your new rotation according to the connected bodies coordinate plane. I do this on Awake, and cache it for as long as the object remains in the scene.

Now say your joint has been moved, rotated etc and you now want to enable it in a new location/rotation. Firstly, cache the new current rotation that you want, then take the initial rotation and rotate it to the coordinate plane of the connected body. You then set the Joint rotation to it, reset the axes, and finally put the joint back to the new current rotation.

 Quaternion currentRotation = joint.transform.rotation; //caches the new rotation that you want the joint to have.
 joint.transform.rotation = Quaternion.Inverse(initialRotation_JointToConnected) * joint.connectedBody.transform.rotation; //Rotates the cached initial rotation to once again match the coordinate plane of the connected body
 joint.axis = joint.axis; //Triggers a limit recalculation
 joint.transform.rotation = currentRotation; //Puts the joint object back to where you want it

I have a script attached to every joint, and all this happens on enable. It's a bit confusing but once you understand the core of what is happening, this makes a lot of sense and can fit any sort of hierarchy joint setup. Hope this saves someone wasted time fussing around with the axes.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

UI won't activate 1 Answer

How to handle inactive object updates 0 Answers

*URGENT* Really Basic Script Problem 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges