Adjust world gravity at runtime?

Is it possible to adjust the world gravity settings at runtime? I know you can GET the values of the World Settings via the level blueprint but it doesn’t let you SET them.

Thanks!

1 Like

Anyone know if this is possible? I want it to affect the gravity of all objects, not just the player so I don’t think a physics volume would work.

1 Like

From what I can tell, for any object that is not located in a specifically placed physics volume, it falls back to the “DefaultPhysicsVolumeClass” that is set in the World Settings.

So, I believe if you wanted a global gravity change outside of placed physics volumes, you’d want to potentially add your own DefaultPhysicsVolumeClass that overrides the GetGravityZ function, where you’d apply your gravity changes at runtime.

I haven’t tried this myself, so not sure if that will work or if it can be done with blueprints alone. Looks like it might require some C++ work to expose ADefaultPhysicsVolume as a BlueprintType, or might be easier at that point to just derive your own DefaultPhysicsVolume in code (or just add a GetGravityZ override directly to ADefaultPhysicsVolume) and expose gravity scale properties used in the overridden GetGravityZ that can be tweaked via blueprint as needed.

Hey, I had a similar situation where it required me to change world gravity at real-time. After searching through, one way to change the global gravity is by using C++. By default UE4 uses ‘Default Gravity Z’ found in Project Settings->Engine->Physics. In order to change the default gravity to user customed gravity, we can make use of WorldSettings.h class in your actor class.
According to the class:

with ‘bGlobalGravitySet’ to true and setting the GlobalGravityZ=‘YourFloatGravity’.

One small way to implement would be:
AWorldSettings* MyWorldSetting=GetWorldSettings();
MyWorldSetting->bGlobalGravitySet=true;
MyWorldSetting->GlobalGravityZ = 900.f;

Hope this helps.

3 Likes

I tried to use this code, but GetWorldSettings is unavailable function for me