How to disable gravity after instantiating and give it gravity back in XR

Hey guys!

I am making a VR game using the XR toolkit. I want to instantiate a gameobject and I want it NOT to move and just float in the air. After I grab it, it should have back the gravity/rigidbody etc. After I instantiate it i use the following code:

gameobject.GetComponent<Rigidbody>().useGravity = false;

When I pick it up and drop it I set the gravity on true which does the job. The problem is, when I don’t grab the object but hover with my hand over it or touch it, it starts flying and floating through the area. How can I make stop that? I’d be grateful if someone can help me out!

Kind regards


You just need also to lock its position and rotation using the rigidbody constraints properties.
You can do it for each axis or freeze them all with:

So it will not be “floating” on space.