- Home /
Hinge joints not reaching target position
I have a hinge that has the following settings:
Rigidbody:
Mass = 1 Ang Drag = 0 Constraints: frozen on all but Y-axis
Hinge Joint:
Spring = 1 Damper = 0.001 Target Position = 0
...
The problem being the hinge joint fluctuates around the target position yet does not stop at 0, but instead +- ~6 degrees. Turning up the spring helps, but fails aesthetically.
Also, in physics I turned sleep angular velocity to zero, which did not help.
I also tried switching the damper to 0 and angular drag to 0.05 (the default), which worked no better.
I wanted to script the hinge myself until I read changing a rigidbody's angular velocity can yield unrealistic results.
[edit:]
Initially, I planned not to share my solution script, but after having to rewrite it, I decided to share it. This script is meant for a western style saloon door, which reacts to the player's movement. Increasing the spring in the HingeJoint and the script will make the door swing faster (and vice-versa) and increasing the damper will make it slow down faster. (I put damper at .1 and spring at 1 for a nice slow western feel). Furthermore, the mass of the player should be at least a magnitude of 3 (ie 1000x) greater in order for the door to perform optimally without getting in the way of gameplay.
#pragma strict
//next 3 are cached variables to increase performance
var Trans : Transform;
var Rigid : Rigidbody;
var Hing : HingeJoint;
//resting position in Eulers.y for the (saloon) door
//ie where it is initially placed in the scene, called at start
var rest : float;
rest = Trans.eulerAngles.y;
function Update ()
{
//this line makes sure we aren't checking when the door is at rest (optimization)
if(Hing.spring.spring < 2)
{
//when the door is barely moving
if(Mathf.Approximately(0,Rigid.angularVelocity.y))
{
//make the door swing faster, also stops sleep
//(we don't necessarily want the door to sleep)
Hing.spring.spring += Time.deltaTime;
//if the door is approximately at resting position
if(Mathf.Approximately(rest,Trans.eulerAngles.y))
{
//make the door stop all angular velocity
Rigid.angularVelocity.y = 0;
//manually place the door at the resting position
Trans.eulerAngles.y = rest;
//manually force the door to sleep
Rigid.Sleep();
}
}
}
}
function OnCollisionEnter ()
{
//THIS IS IMPORTANT
//if this weren't here, the door spring would increment to infinity
//making it swing super fast in the future, compounded each time the door is hit
//AND continue to reach approximately plus or minus 6 degrees, not perfect equilibrium position
Hing.spring.spring = 1;
}
Answer by Melanina · Jun 14, 2013 at 10:37 AM
I have the similar problem, because i use too small scale in my project ( unity have finite accuracy ) just try put your object to another empty object and rise him scale to see difference. I hope someone know how to change this defect without scale manipulation.