- Home /
Question by
CookieCreator · Mar 19, 2017 at 07:20 PM ·
rigidbodyvrgravity
How to toggle gravity on a rigidbody object?
My current code requires a key to be held down to hold an object which turns off gravity and follows the controller (This is VR) When I let go gravity is reapplied and it stops following the controller.
private void OnTriggerStay(Collider collider)
{
item = collider.gameObject;
if (controller.GetPressUp(gripButton))
{
item.GetComponent<Rigidbody>().useGravity = true;
gameObject.GetComponent<Renderer>().enabled = true;
}
if (controller.GetPressDown(gripButton) && item)
{
item.transform.parent = this.transform;
item.GetComponent<Rigidbody>().useGravity = false;
gameObject.GetComponent<Renderer>().enabled = false;
}
}
private void OnTriggerExit(Collider collider)
{
item.transform.parent = null;
}
However I need it so that on first click it will pickup the object and on second click let go. I am also not just looking for the code to be given to me some commenting would be nice.
Thanks for the help!
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612113508im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Handling Large number of Active Rigidbody Evaluations 2 Answers
Moving in VR using planetoid-like center of gravity 0 Answers
Gravity.cs help? 3 Answers
Artificial Gravity 2 Answers
Custom gravity based on normal vector and rigidbody.addForce 2 Answers