How to get Target Position to follow another Hinge joint Angle?

Hi , in my project I have to make an hinge target position to be equal to the angle of another hinge.

Let me explain: I have two arms and I would like the second one to attempt to reach the same angle as the first one while using physics and collision. (This is why I use hinges).

The player can move the arm 1 which is not using the spring feature of the hinge (this arm move but doesn’t react to physics as designed)

Then the second arm should respond by attempting to reach the angle of the (arm 1) hinge joint.

The expected result is that both arm have the same angle when it is possible (collisions might occur on arm 2 but spring will react with the target position)

So I am using the Target Position on arm 2 by copying the arm 1 Angle.

What I understand is that the “angle” variable of a hinge joint correspond in it’s distance from his starting position BUT it doesn’t give the good results.

So here is what I attempted to do in order to understand my problem:

  • Create 2 cubes
  • Add Rigidbody to both cubes.
  • Select cube 1 and freeze his rigidbody rotation and position.
  • Select cube 2 and add a hinge joint.
  • Set it up to rotate around the y axis so no gravity will affect it’s position.
  • Set those values on the hinge joint from the inspector:

Use spring = true

Spring spring = 150

spring damper = 150

target position = 0

  • Attach a script to the cube with the hinge that will Print out the “Angle” variable from the hingejoint.
  • Launch the game and modify the target position at runtime while keeping an eye on the angle value.

While doing this I noticed that when entering 100 as target position the angle printed would be 98-100 as expected BUT when making the second change the result start breaking.
When setting the target position to 0 the angle jumps from 100 to -76…

If anyone can help me to get the proper angle out of my joint I would really be happy.

Thank you very much for your help!

(This is the code to print the Hinge Angle)

private HingeJoint myJoint;
void Start(){myJoint = GetComponent<HingeJoint>();}
void Update () {Debug.Log(myJoint.angle);}

I am also getting crazy angle values from my hinge joint.
Anytime I change the target velocity on the motor it seems to reset the angle to 0 at it’s current rotation. Trying to work around that :expressionless:

The target velocity needs to be a value between -180 & 180. If it goes under or above, it clamps to 0.

Quick fix:

    angleBeforeDrag = transform.rotation.eulerAngles.z;
    if (angleBeforeDrag < -180f) angleBeforeDrag += 360f;
    if (angleBeforeDrag > 180f) angleBeforeDrag -= 360f;