Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

HingeJoint guru needed- questions about parenting, and HingeJoint.angle

Discussion in 'Physics' started by sgower, Feb 10, 2017.

  1. sgower

    sgower

    Joined:
    Mar 9, 2015
    Posts:
    316
    Hello, I sure hope someone can help! I'm trying to make the below object work in VR (HTC Vive).
    One controller will hold the blue handle (as shown), while the other controller can push the lever back and forth:



    Problem #1.
    The HingeJoint (the cylinder) Connected Body is the Rigidbody within the blue handle. What I'm seeing is that when the Connected Body moves, then HingeJoint.angle also moves. I'm trying to calculate lever position (the Hinge joint has min=-40 and max=40). It works fine as long as the Connected body doesn't move, but the HingeJoint.angle values change wildly as you move the controller around. Can someone explain how I can get an accurate reading of the lever position?


    Problem #2. I'm very confused about to set up the child/parent relationships between the various objects.
    Can the HingeJoint Object (The lever), be a child of the object containing the Connected Body Rigidbody, or is this a definite No-No? Currently I have the joint as a child which I think must be incorrect. The Connected Body Rigidbody (the handle) is Kinematic, and the HingeJoint isn't (it can't be correct?). Physics doesn't work when you have a non-kinematic child, or a kinematic parent, so my current approach must be wrong. This contraption is a prefab which is why the lever is a child, but maybe I need to unparent the HingeJoint object at runtime?

    Also, I could change the Handle ConnectedBody to be non-kinematic, but, because it needs to be moved by the Vive controller, I'd need to attach it via a FixedJoint (which is fine).

    If someone could briefly outline a scenario explaining how the child/parents should be set up, and how the rigidbodies need to be set up, I'd greatly appreciate it. Also, I need a solution for problem #1, but maybe fixing Problem#2 will also fix Problem #1. ??

    I've burned through a ton of time trying to figure this out, so even if you could point me in the right direction, it would help a lot. Thanks!
     
  2. sgower

    sgower

    Joined:
    Mar 9, 2015
    Posts:
    316
    I ended up finding a properly functioning VR lever in this (great) Asset: https://www.assetstore.unity3d.com/en/#!/content/69323

    Some notes:
    • TheleverKnob is connected to the leverShaft via a Fixed Joint. The leverShaft is also connected to the hinge via a Fixed Joint. Interestingly (as you can see), both of these objects are children of the hinge. I was under the impression that when using a FixedJoint that the object shouldn't have any child/parent relationship. I guess I was wrong about this? If anyone can shed light on this, please do!
    • The hinge (with it's non-kinematic Rigidbody) is a child of "lever_yellow_1" which is the "Connected Body" (and Kinematic) Rigidbody object. Again, the hinge is a child of the Connected Body object. I thought that you should never have parent and child objects that both have Rigidbodies. I also thought that it is especially bad to have a mix of Kinematic and non-kinematic rigidbodies between parent objects and child object. Are HingeJoints and FixedJoints exceptions to these rules?
    • With the current setup, I'm not seeing correct HingeJoint.angle values even when I move the root object (handle1)
    • In the example below, all the objects between the handle1 and lever_yellow_1 aren't relevant to this discussion. I can delete a couple of these.

     
  3. FaberVi

    FaberVi

    Joined:
    Nov 11, 2014
    Posts:
    145
    Problem #1.
    The HingeJoint (the cylinder) Connected Body is the Rigidbody within the blue handle. What I'm seeing is that when the Connected Body moves, then HingeJoint.angle also moves. I'm trying to calculate lever position (the Hinge joint has min=-40 and max=40). It works fine as long as the Connected body doesn't move, but the HingeJoint.angle values change wildly as you move the controller around. Can someone explain how I can get an accurate reading of the lever position?

    Same problem... Any solution for this?
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,511
    Please just create your own threads, they're free and easy to do. Threads get messy when they contain multiple devs problems, assumptions they're all about the same thing etc.
     
  5. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,500
    Please Create an appropriate tutorial!
    https://archive.org/details/3dbuzz-...ls-part-04/020-physics/010-10-hinge-joint.mp4

    this video is ten years old but pretty cool. Unity must do one. Is 10 yrs late!


    Probably you need to use a configurable joint with similar masses for each game object.
    Mass is important. If there is too much difference, it does not work. Exapme:

    The video example and solution is for Unreal but are exactly the same for Unity.

    Follow the Unity 3DBuzz series using the links I pass you to get how parent-child relation works in Unity.
     
    Last edited: Aug 17, 2022
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,511
    Well I don't work on the 3D physics nor Learn team but okay, I'll get right on it. ;)