Hi guys,
I’m struggling with this problem for days now and would really appreciate if someone could help… ![]()
I’ve got a gameobject with a rigidbody and a configurable joint as components. In the editor I can set the angle that I want the joint to rotate to (angleToRotateTo). If angleToRotateTo is changed the following coroutine which is meant to stepwise rotate the joint to the desired angle is started. A flag is set (isRotating) to prevent that the coroutine is started multiple times. Then the UnlockJoint-Function is called which unlocks the joint. The following while-loop should rotate the joint to an angle +/- 0.005 the desired angle. When the loop finishes the joint is locked again, flag is set to false and the coroutine is finished.
IEnumerator RotateJointToTarget()
{
isRotating = true;
UnlockJoint (joint, rb);
while (Mathf.Abs (angleToRotateTo - currentAngle) > 0.005f)
{
rb.WakeUp ();
if (currentAngle < angleToRotateTo)
targetRotation += FindRotationStep (currentAngle, jointRotationStep, angleToRotateTo);
else if (currentAngle > angleToRotateTo)
targetRotation -= FindRotationStep (currentAngle, jointRotationStep, angleToRotateTo);
joint.targetRotation = Quaternion.Euler (targetRotation, 0f, 0f);
yield return new WaitForFixedUpdate ();
}
LockJoint (joint, ref targetRotation);
isRotating = false;
}
The FindRotationStep-function is shown below. It changes the jointRotationStep which is 0.09f in my current setup to 0.001f as soon as the delta to the desired angle is less than two times the usual jointRotationStep (0.09f).
public float FindRotationStep(float curRotationVar, float jointRotationStepVar, float angleToRotateToVar)
{
float newJointRotationStep = jointRotationStepVar;
if (Mathf.Abs (angleToRotateToVar - curRotationVar) < 2f * jointRotationStepVar)
newJointRotationStep = 0.001f;
return newJointRotationStep;
}
It works fine: The targetRotation-variable is incremented / decremented by 0.001f when the condition is reached. But I noticed that the currentRotation (which comes from the rigidbodies rotation) is incremented / decremented by 0.09f for about 4 further fixedUpdates before the Increment / Decrement is reduced. Do you guys have any idea why this happens? I tried to set the rigidbody kinematic after the condition is reached and also set its velocity and angularVelocity to zero but nothing helped. I have no clue what else to do and hope for some good advise now.
If you need any further information - just let me know and I’ll provide it.
Best regards
Mathias
