I'm looking to try and implement a feature in my game where a keyboard press sensor would reverse the gravity of the scene to -9.8 any ideas on how I could do this? I tried playing around with force fields which didn't work and material force fields which didn't have the desired effect. Is there someway that I could edit the gravity value with python scripting?Simple recreation of what I'm trying to do. I would like the keyboard actuator to be able to toggle gravity.

  • $\begingroup$ May be you could reverse the mass of the object/s instead of the gravity? $\endgroup$
    – user60781
    Commented Sep 3, 2019 at 3:08
  • $\begingroup$ I tried that already, mass can only be a positive value. $\endgroup$
    – Jake G
    Commented Sep 3, 2019 at 3:10
  • $\begingroup$ Hmmm....reversing "up" ... that is: The direction of the Z-axis? $\endgroup$
    – user60781
    Commented Sep 3, 2019 at 3:47

1 Answer 1


Welcome to Blender Stack Exchange!

I believe Python is the easiest way to achieve what you are looking for.

If I understand you correctly, you only need to switch the value once, not back and forth, which is quite easy to do. Simply write these two lines in the text editor and save it somewhere:

import bge
bge.constraints.setGravity(0, 0, 9.8)

Note that by default, gravity is set to negative 9.8, so positive 9.8 is actually inverted.

Then, go to whatever object you want, for example the player if you have one, and add a Keyboard sensor connected to a python controller. Choose the script you just wrote, and you should be good to go.

EDIT: While the code above will invert gravity, it can't switch it back. This will explain how to make gravity switchable as many times as you like.

Initial Setup

This is my initial setup. As far as I am aware, Python is the only way to mess around with the value of Gravity, so that's what we'll use. Make a new text file in the text editor and call it whatever you want. Since it's python code, you can end it with .py to indicate that, but it's not necessary.

In the logic editor I added two sensors connected to a python controller. The script will only have access to the sensors and actuators connected to the python controller. Then I selected the script (which is currently blank) in the menu there.

On the always controller, enable the 3 raised dots icon. This will run the script every time the game updates (I think it's 60 times per second by default). For the keyboard sensor, make sure you know what it's named. I called it "switch" since when that key is pressed gravity will switch directions. The reason the name is important is we'll have to reference this sensor by name later on. Also enable Tap, as this will prevent weird behavior when holding the button.

I also set the cube to be a rigid body, so that it would obey gravity and we can see whats going on.

Here's the script that we can now use to invert the gravity. Note that to work the keyboard sensor needs to be called "switch", or you have to change the name in line 4 to the name of your sensor. Either works just fine. Anything with a # symbol in front is a comment, and is only there to explain what's going on.

#Tell Python to get access to all the BGE commands that are built into Blender
import bge
#Tell Python to access the controller that is running the script.
cont = bge.logic.getCurrentController()

#Tell Python to look for a connected sensor named "switch"
switch = cont.sensors["switch"]

#If the "switch" sensor is activated, run the code after the colon.
if switch.positive:
    #Tell Python to get the current gravity values.
    grav = bge.logic.getCurrentScene().gravity
    #Tell Python to set the gravity to the same values that it just got
    #but make the z axis the negative of whatever it was.
    bge.constraints.setGravity(grav[0], grav[1], -grav[2])

You can stop here, but I explained how the script works a bit so you can figure out how to change gravity further in the future if you like.

This script waits for the space bar to be pressed. When the space bar is pressed, the bge.logic.getCurrentScene().gravity gets the current gravity values on all axes and stores it in the grav variable as an array. An array is simply a list. It would look something like this:

[0, 0, -9.8]

This means gravity will not move things on the x or y axes, but will on the z axis. Since we assigned this to a variable, whenever we now use this variable, it uses the array that we assigned to it instead.

Now all we have to do is set the gravity, but invert the z axis value. The bge.constraint.setGravity() lets us do this. We just need to tell it what values to use. We can tell python to use a value from a certain place in an array by giving it the array, then the index (the first number is at position 0).

We can plug these into the command above. grav[0] is the x value of gravity (0), grav[1] is the y value (also 0), and grav[2] is the z value (-9.8). Since we want to flip the gravity, we just tell Python to set the gravity to the opposite of what it currently is.

  • $\begingroup$ I was looking for a way to make an actuator within the Logic Editor. I want their to be a keyboard input that can switch the gravity value from -9.8 to +9.8. (I've attached an image.) $\endgroup$
    – Jake G
    Commented Sep 3, 2019 at 3:22
  • $\begingroup$ I edited my answer to explain how to use the script in the Logic Editor. If you need the gravity to be switched back and forth, that is possible too, and I can explain that if you want. $\endgroup$
    – Brenticus
    Commented Sep 3, 2019 at 3:32
  • $\begingroup$ Yes I would like to switch the gravity back and forth. $\endgroup$
    – Jake G
    Commented Sep 3, 2019 at 9:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .