Change worlds gravity vector

Godot Version

v4.3.stable.official [77dcf97d8]

Question

Hi
I want the player to enter an area which triggers an inverted gravity of the whole world (3d).

I read multiple other threads about similar issues, but was not able to find a solution.

If I go to project settings > physics > 3d
I can find the default gravity and the default gravity vector.
I would like to change the default gravity vector during runtime.

If I hover above the setting It even gives me some code:

But

PhysicsServer3D.area_set_param(get_viewport().find_world_3d().space, PhysicsServer3D.AREA_PARAM_GRAVITY_VECTOR, Vector3(0,1,0))

Does not have any effect…

Could anybody tell me how I can make this work?

:pray:

Oh I realized, actually both following ways work:
ProjectSettings.set_setting("physics/3d/default_gravity_vector", Vector3(0,1,0))
or
PhysicsServer3D.area_set_param(get_viewport().find_world_3d().space, PhysicsServer3D.AREA_PARAM_GRAVITY_VECTOR, Vector3(0,1,0))

But all my objects who are stationary will not get an impulse. Only if I jump or if a RigidBody object is moved they fly all up.
How can I make sure that all elements get informed about the gravity change and start to fly away?

Add them to a group of any name you want and use get_tree().call_group()

In any script that needs to react the gravity change:

func _ready() -> void:
	add_to_group("gravity_change") # Can be any other name you want


func gravity_changed() -> void:
	# Do what you need to do when the gravity change

And when you do the gravity change you can call:
get_tree().call_group("gravity_change", "gravity_changed")

1 Like

Thanks for your quick answer.
Now let’s assume I have a few RigidBodies3D. They already fell onto the ground. Now I execute the gravity_changed() function… but what should be in there?
I don’t wan’t do apply forces, I just want that physics take over.

Or could i trigger that by adding just a tiny tiny impulse?

Thanks for further help :sweat_smile:

Yeah, a tiny impulse to awake them should work

1 Like