- Home /
Making my character's gravity change when i hit a collider.
In my game I have a water plane and I cant figure out how to make it to where when I hit the box collider my character's gravity will change allowing me to swim in the water and also when I leave the water my gravity with change back to normal.
Answer by BiG · Jun 21, 2013 at 07:29 AM
If you just want to turn gravity on and off, you can use Rigidbody.useGravity. See this example, it should be a good starting point for your needs.
However, when you turn the gravity on, consider that the particular rigidbody will be affected by the "global" gravity of your world, that's the value defined in Physics.gravity. I don't think that is what you want, but (just in case), if you want the object to respect a "particular" gravity value, you should calculate that accordingly, through its mass.
I have written a script to disable gravity OnTriggerEnter and enable gravity OnTriggerExit but the script doesn't do anything, here is what it looks like. var useGravity : boolean;
function OnTriggerEnter(other : Collider){
if(other.attachedRigidbody){
other.attachedRigidbody.useGravity = false;
}
}
function OnTriggerExit(other : Collider){
if(other.attachedRigidbody){
other.attachedRigidbody.useGravity = true;
}
}