- Home /
Manipulate gravity in an area?
Hi, I'm trying to make something like a gravity bomb ( When a rigidbody enters a certain trigger or area its gravity decreases ). How do I access the "Use Gravity" option in the rigidbody? Or Is there other ways I can achieve this gravity-bomb thing? Please help in JS, Thanks!
Answer by save · May 29, 2011 at 10:46 AM
You could use something similar to this on the explosion position (instantiate an empty gameobject for instance with this script at the bombs explosion position):
var radius : float = 5.0;
var liftForce : float = 10.0;
function Start () {
// Disable gravity for objects inside explosionPos+radius
var explosionPos : Vector3 = transform.position;
var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);
for (var hit : Collider in colliders) {
if (!hit)
continue;
if (hit.rigidbody)
hit.rigidbody.useGravity = false;
hit.rigidbody.AddForce (0, liftForce, 0);
}
//Destroy this object after it's done
Destroy(gameObject);
}
Can't you just disable gravity for an object that OnTriggerEnter?
Yes sure that would work too! But this adds possibility to also do it in update where the radius expands to procedurally disable gravity (which probably would look really good in action).
edit: On the other hand the trigger area could also be resized over time which would give the same result - so either this or Johan 4's suggestion. ;)
Will certainly try these after I get home, thanks for the answer and replies!
Oh, another thing, how do I activate/deactivate the rotation/position constrains available in the rigidbody options? Is it possible?
Of course, this is Unity we're talking about. :)
http://unity3d.com/support/documentation/ScriptReference/Rigidbody-constraints.html
http://unity3d.com/support/documentation/ScriptReference/Rigidbody-freezeRotation.html