Time Freeze

hello i am trying to freeze my cube with script so when press R they have to freez in the position that they are this code is working but behaves very strange sometimes they countiniusly moove when freezed.

if (Input.GetKeyDown (KeyCode.R)) {

	GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezeRotationZ;
	GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezeRotationY;
	GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationX;
	

	}
	if (Input.GetKeyUp (KeyCode.R)) {
		// the direction to leap towards mouse
		Physics.gravity  = new Vector3 (0, -6.0F, 0); 
	}

Another way to achieve what you want is to make the rigidbody kinematic when you press the key.

GetComponent<RigidBody>().isKinematic = true;

Did you tried to not constraint the gravity direction ex:Y or Z?

Well, you set the constraint flags 3 times right after each other, overwriting the previous flags. So at the end you only have the body constrained with FreezePositionZ | RigidbodyConstraints.FreezeRotationX; I believ you want to constrain all six at the same time right? Then you need to “or” all of the flags. Or simply use FreezeAll…

Also, gravity has nothing to do with freezing of objects. No idea what the purpose of the gravity code is though.