How to set 3D physics gravity programatically

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Juxxec

Greetings!

How can I change the gravity of my entire scene programatically?

I saw that I can use the PhysicsServer to achieve this. However, when I change the gravity vector, the gravity stops working altogether.

This is the code I am using:

PhysicsServer.area_set_param(get_world().get_space(), 
    PhysicsServer.AREA_PARAM_GRAVITY_VECTOR, gravity_dir)
PhysicsServer.area_set_param(get_world().get_space(), 
    PhysicsServer.AREA_PARAM_GRAVITY, 9.8)
:bust_in_silhouette: Reply From: Zylann

There is a bug open about this: [Bullet] Changing 3D gravity in BulletPhysics does not work as expected · Issue #35378 · godotengine/godot · GitHub

It works if you change the physics engine to GodotPhysics in your project settings.

Another alternative which doesn’t involve changing the physics engine is to turn off gravity, and apply its force yourself.