You can toggle rigidbody.useGravity: Unity - Scripting API: Rigidbody.useGravity
Maybe by setting it true and false in OnTriggerEnter/OnTriggerExit. This can get weird if you have overlapping triggers, though. (You might enter a new trigger and then exit another trigger after, and still be inside the new trigger). It’s the easiest solution, and will apply to all rigidbodies equally, but requires your gravity zone things be spaced pretty far apart.
A more robust solution would be to keep gravity disabled permanently and have your character always apply its gravity forces. The downside here is that you’d need to add a script to every object that you want affected by the zones. (Or could do it programmatically somewhere).
This more robust solution would look like:
The execution order page is here: Unity - Manual: Order of execution for event functions
and the physics timestamp is:
Relative to the actual physics simulation, OnTriggerStay is actually before FixedUpdate.
I’d recommend your character have some kind of ManualGravity script, with a simple Vector3 gravity parameter exposed. That script’s FixedUpdate would look like:
// apply whatever we had stored this frame
rigidbody.AddForce(gravity, ForceMode.Acceleration);
// default gravity value here
gravity = new Vector3(0f, -9.8f, 0f);
Note that the default gravity is set after it’s applied. Before the next time FixedUpdate could run, other scripts and events would be able to modify the gravity. Your OnTriggerStay in the gravity zones would just do:
public Vector3 gravityForce = new Vector3(0f, -9.8f, 0f)
function OnTriggerStay (other : Collider)
{
var manualGravity = other.GetComponent<ManualGravity>();
if (manualGravity)
{
manualGravity.gravity = transform.TransformDirection(gravityForce);
}
}
Note that you could complicate this even further by doing a List gravityForces, or something, and then each zone would add to that list. This would let you have overlapping zones that behave like they do in the super simple solution above. (You’d just apply a normal gravity force if that list was empty).
Hope this helps!
P.S. Ignoring the more advanced solution there, if you’re going to never overlap gravity zones just do the OnTriggerEnter/OnTriggerExit toggle of rigidbody.useGravity! I just included the other example here as a more robust way to handle overlapping triggers.