OnTriggerEnter Gravity script not working

I made a box collider and put a script in it

function OnTriggerEnter (other : Collider) {
if (other.attachedRigidbody) {
other.attachedRigidbody.useGravity = false;
}
}
collider.isTrigger = true;

so anything inside this box should be 0 gravity right? but it’s not working :frowning:

I think the problem is that this is just disabling the gravity option, it’s not setting gravity to 0 like I want. How would I do useGravity = true; and then gravity = 0 for this box? and when you exit the box gravity = 1 or whatever.

Not quite. You can't set gravity for a single specific object, although you can turn it completely off. Remember that if an object is falling, it will retain its velocity when gravity is disabled- so unless it has a really high drag value, there's nothing to stop it moving after it enters your zero-G field!

To get things to reactivate when they leave, just do the same for OnTriggerExit, only with

other.attachedRigidbody.useGravity = true;

What exactly is happening? How are you trying to use this script? Other than formatting (which I'll just assume is UA screwing things up when you tried to copypasta from your text editor), it seems to be functional code.

Of course, to turn gravity off completely you would use Physics.gravity = Vector3.zero;

oh sorry, yeah it was a copypasta issue here.

here is more of what I am trying to accomplish (for only objects within the field)

function OnTriggerEnter (other : Collider) {
if (other.attachedRigidbody) {
Physics.gravity = Vector3.zero;
}
} 
collider.IsTrigger = true;

this seems to work somewhat, better than what I had going on before :wink:
so now when a rigidbody enters the anti-gravity room, Physics.gravity = Vector3.zero; is setting the gravity the way I wanted, but it’s doing this for global objects and not just what’s inside the bounds of the scripted object.

so I need a script that will apply these rules purely to objects within the anti-gravity area.

could “Physics.gravity = Vector3.zero;” be somehow applied to the rigidbody(s) which resides within the bounds of the scripted object?

I tried the following non-functioning code with no success.
function OnTriggerEnter (other : Collider) {
if (other.attachedRigidbody) {
other.attachedRigidbody.Physics.gravity = Vector3.zero;
}
}
collider.IsTrigger = true;

but ‘Physics’ is not a member of UnityEngine.Rigidbody

I need some serious lessons in UnityScript scripting :wink: