HingeJoint2D

Ok, two questions. I have a rectangle sprite with a hingejoint2d on the bottom middle of it. Objects can land on either side, creating a seesaw effect, however, I need the rectangle to rotate back to its original position after. Is this possible without using something like a springjoint?

I tried a few things with code, but I had a problem where dropping something on the left side worked fine, but the right side would cause the rectangle to spin around and turn upside down. Not the effect I wanted.

The second question is how do I access the hingejoint if I want to change limits?

Thanks in advance.

Regarding having it rotate back to its initial position…

  1. Depending on how you have the intial position setup, you could simply increase the angular drag of its rigidbody so that it settles back into place, based on gravity
  2. If the above doesn’t work, then in the FixedUpdate method you could repeatedly check the angle of the rigidbody, and increase the motor speed of the HingeJoint until it gets back to the desired angle, at which time you would set the motor speed back to zero

Regarding changing the limits through code…

  1. In a script create a public variable…
    public HingeJoint2D myHingeJoint;
  2. In the Inspector, drag the HingeJoint2D onto this variable of this script. Now you have access to the HingeJoint.
  3. In the script, use the methods of the HingeJoint in order to modify the HingeJoint. In this case it would be something like…
    myHingeJoint.limits.min = 0;
    myHingeJoint.limits.max = 90;