Search Unity

How Do I Enable Object to Follow Axis Rotation Using Hinge Joint in a Moving Object

Discussion in 'Physics' started by RendCycle, Jul 29, 2018.

  1. RendCycle

    RendCycle

    Joined:
    Apr 24, 2016
    Posts:
    330
    REVISED QUESTION:

    How do I limit rotation of Object Moved by Gravity?


    I have a Child object, an Iron Bar (with Rigidbody and Gravity), connected to a Long Arm (w/ Rigidbody and isKinematic). I need to restrict the rotation of the Iron Bar from 1 to 40 degree angles only so it won't go overboard and hit the Long Arm. Please refer to this image for more info:



    I tried several approaches from using a Hinge Joint and its Limit options and also through code. But I cannot seem to solve this problem. Right now I have this script attached to the Iron Bar but it does not seem to have any effect.

    Code (CSharp):
    1. public Transform targetTransform;
    2. private Vector3 _currentAngle;
    3. private Vector3 _targetAngle;
    4. float rotationX;
    5.  
    6. void FixedUpdate () {
    7.  
    8.     transform.right = targetTransform.transform.right; //-- To make the Iron Bar follow player rotation
    9.  
    10.     rotationX = transform.eulerAngles.x;
    11.  
    12.     if (rotationX > 40) {
    13.         _targetAngle = new Vector3 (40, transform.eulerAngles.y, transform.eulerAngles.z);
    14.         _currentAngle = new Vector3 (Mathf.LerpAngle (transform.eulerAngles.x, _targetAngle.x, Time.deltaTime), Mathf.LerpAngle (transform.eulerAngles.y, _targetAngle.y, Time.deltaTime), Mathf.LerpAngle (transform.eulerAngles.z, _targetAngle.z, Time.deltaTime));
    15.         transform.eulerAngles = _currentAngle;
    16.     } else if (rotationX < 1) {
    17.         _targetAngle = new Vector3 (1, transform.eulerAngles.y, transform.eulerAngles.z);
    18.         _currentAngle = new Vector3 (Mathf.LerpAngle (transform.eulerAngles.x, _targetAngle.x, Time.deltaTime), Mathf.LerpAngle (transform.eulerAngles.y, _targetAngle.y, Time.deltaTime), Mathf.LerpAngle (transform.eulerAngles.z, _targetAngle.z, Time.deltaTime));
    19.         transform.eulerAngles = _currentAngle;
    20.     }
    21.  
    22. }
    Would appreciate any help you can provide. Thanks for taking a look. What follows below is my original question before I partially solved some of my problems.

    INITIAL QUESTION:

    How Do I Enable Object to Follow Axis Rotation Using Hinge Joint in a Moving Object?

    I'm new in working with Unity's Hinge Joints but have already spent a few days trying to solve this problem. I just need one object with Gravity to swing in the X Axis while rotating in the Y/Z Axis or rather just follow the correct Player orientation when he moves. I have the following:

    OBJECT A: LONGARM

    This object has the HINGE JOINT script and is connected to other arms that are set to isKinematic and has NO Rigidbody. The full set of Arms are then connected to a player-controlled Vehicle with Rigidbody that can move/travel from one place to another. This is the Parent of Object B.

    OBJECT B: ITEMHOLDER

    This Child object is specified as the CONNECTED BODY of the Hinge Joint Object A and has Gravity and Rigidbody.

    For more information on the current Inspector configuration of each object, kindly refer to the attached image.


    I have already tried several configurations (especially using Constraints) and have even used Configurable Joint, ticked Enable Adaptive Force, and increased both Solver Iterations to 7+ in the Physics Inspector. I have also tried moving the related objects to different Hierarchy levels and even used another empty GameObject to act as in-betweener for Object A & B. But still, I cannot find the proper answer. Would appreciate any help you could provide. I'm using Unity 2018.1.6 by the way.

    Note: I'm not sure if I still need to create a script for the ItemHolder object to do this as I am assuming my requirements can already be handled by Unity's Hinge Joint script? That is its purpose right?
     

    Attached Files:

    Last edited: Aug 1, 2018
  2. RendCycle

    RendCycle

    Joined:
    Apr 24, 2016
    Posts:
    330
    I am guessing Unity's Hinge Joint system is totally broken, messed up, and unusable for 2018.1.6. Trying to use it is going through a lot of trial and error torture testing. I haven't found anybody else in the Internet who has claimed that they have used it successfully unless its just for ropes and a very simple non moving parent. :( For such a finicky thing to work with, I would've expected more detailed tutorials has already been made available by Unity and others.
     
    Last edited: Jul 30, 2018
  3. RendCycle

    RendCycle

    Joined:
    Apr 24, 2016
    Posts:
    330
    I was able to make the Child object (ItemHolder) follow the rotation of the parent Player object by simply using the line of code below under FixedUpdate and locking all Rigidbody Constraints of the Child except X Rotation:

    Code (CSharp):
    1. transform.right = targetTransform.transform.right;
    This somehow worked but any adjustments to the Hinge Joint script like limiting the rotation (min/max) does nothing and has no effect whatsoever. So my new problem now is how to avoid the Child object from rotating 360 degrees in the X Axis and just limiting it to 0-90 degrees.

    I am also guessing that this will work similarly without the use of a Hinge Joint and just relying on Rigidbodies and the proper Hierarchy object placements. But I was aiming at using the Hinge Joint Limits... but found out its useless and still does not work as intended for my use case. Anybody knows how to add X Axis Rotation limits through code?
     
    Last edited: Jul 31, 2018
  4. RendCycle

    RendCycle

    Joined:
    Apr 24, 2016
    Posts:
    330
    Revised the question for clarification purposes as I have already solved some of the problems I have. But I cannot seem to edit the thread's Title so I left it as is.