- Home /
Can't change HingeJoint properties by script
I have this script attached on a gameobject with a Hinge Joint. When click up/down, the value is changed. The debug shows the new value, but nothing happens on Inspector and gameobject still doesn't move. If I put same values by inspector, works. What to do?
public class Mover : MonoBehaviour
{
JointMotor motor;
void Start()
{
motor = GetComponent<HingeJoint>().motor;
motor.targetVelocity = 5;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
motor.force += 2f;
else if (Input.GetKeyDown(KeyCode.DownArrow))
motor.force -= 2f;
Debug.Log("Force:" + motor.force);
Debug.Log("Vel:" + motor.targetVelocity);
}
}
Answer by kinggryan · May 12, 2015 at 09:48 AM
I've also encountered this and devised a workaround. I'm not sure, but it seems like changing the constraints of a joint do not cause the physics engine to calculate a solution for the scene. By "prodding" the object whose joint's constraints have been changed, you can force a solution. For me, after changing the constraints of a joint, I called AddForce(Vector3.zero) on the object's rigidbody. This caused the new constraints to be enforced.