The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

+1 vote

How do you change global gravity properties from code at runtime?

Thought it might be possible from a custom integrate_forces method where the physics state is passed to you, but only see get_total_gravity(); no setter there. Was hoping gravity could be accessed/changed from a normal fixed_process method.

Thought there may be a way to reference a "default" Area2D in a scene which would also allow gravity to be changed, but can't find a way.

Is there an easy way to do this, before I kludge something together with these various options?

in Engine by (13 points)
edited by

1 Answer

+5 votes

with this kind of code I have changed the global gravity in my game

Physics2DServer.areasetparam(getworld2d().getspace(), Physics2DServer.AREAPARAMGRAVITYVECTOR, Vector2(0,0))

by (699 points)

Note that you can also set AREA_PARAM_GRAVITY (which is a scalar) if you just want to change the strength.

Underscores seem to have disappeared from this answer. I think it should be:
Physics2DServer.area_set_param(get_world_2d().space, Physics2DServer.AREA_PARAM_GRAVITY_VECTOR, Vector2.ZERO)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.