- Home /
Turning on and off Rigidbody programmatically?
Here is a code snippet I found that should turn off and on gravity (changing the bool)
function OnTriggerEnter (other : Collider)
{
if (other.attachedRigidbody)
{
other.attachedRigidbody.useGravity = true;
}
}
I would like to know how to make it turn on the gravity of a specific object when another object collides with the trigger. THanks in advance.
Answer by Mickydtron · Dec 22, 2010 at 09:10 PM
So you want to do what that code does, but only selectively, instead of for everything. That would be:
function OnTriggerEnter (other : Collider)
{
if ((other.attachedRigidbody)&&(other.tag == "gravObject"))
{
other.attachedRigidbody.useGravity = true;
}
}
Or if you want the gravity to change for ObjectA when ObjectB hits TriggerC. which would be:
var gravObject : Transform;
function Awake() { gravObject = GameObject.FindWithTag("GravObject").transform; }
function OnTriggerEnter (other : Collider) { if (gravObject.rigidbody) { gravObject.rigidbody.useGravity = true; } }
Answer by heks · Dec 22, 2010 at 09:11 PM
Create a reference to the GameObject
that's supposed to get gravity and reference that in your trigger:
var gravityObject : GameObject;
function OnTriggerEnter(other : Collider) { if(gravityObject!=null && gravityObject.rigidbody!=null) gravityObject.rigidbody.useGravity = true; }
Answer by Mark 6 · Dec 22, 2010 at 09:15 PM
That code turns the gravity on to the "other" which is the gameObject which has entered the gameObjects trigger. Have you got RigidBody attached to the other gameobject ? with gravity unchecked ?
Your answer
Follow this Question
Related Questions
Rigidbody changing Gravity in Code? 0 Answers
Setting Horizontal Gravity 1 Answer
rigidbody.Velocity stops gravity 2 Answers
How can I toggle gravity for a single rigidbody? 1 Answer
Applying force to a rigidbody 2 Answers