High Gravity Room

Godot Version

4.3

Question

how do i make an area 2d that increase my character's gravity when entered and turn it back to normal when exited without having to reconnect signals every level i want to use it again

Hi,

I suppose you’re using a CharacterBody2D node with a gravity value inside of it that you’re applying to the velocity. You can detect entering and exiting 2D areas by using signals such as area_entered and area_exited, and connect to those signals functions that will change the gravity scale of your character.


For instance, you could declare two values in your character script:

var gravity_scale: float = 10
var high_gravity_scale: float = 50

Plus a boolean to know if the character is currently in a high gravity zone or not:

var high_gravity_zone: bool = false

Then, you connect two functions to your player collisions (I don’t know exactly how your character node is set up so you may have to work on it on your side) that will just set the boolean previously declared:

func on_high_gravity_area_entered():
    high_gravity_zone = true

func on_high_gravity_area_exited():
    high_gravity_zone = false

And finally, when applying gravity, you use the desired value based on the high gravity state:

var target_gravity_scale = high_gravity_scale if high_gravity_zone else gravity_scale
velocity.y = target_gravity_scale  # That may defer in your code, it's just an example.

That’s a basic example on how you could do that. Let me know if that helps or if you need more details.

1 Like

heres my code so far, i need more details because i dont understand how last step work

The last part is just a computation of the actual gravity you want to apply.
That instruction:

var target_gravity_scale = high_gravity_scale if high_gravity_zone else gravity_scale

could be written as:

var target_gravity_scale = gravity_scale
if high_gravity_zone:
    target_gravity_scale = high_gravity_scale 

Basically: assign the target gravity to apply to its default value, and if the player is in a high gravity zone, then assign it to the high gravity value.

In your code, you could write something like:

if not is_on_floor():
    var target_gravity_scale = gravity_scale
    if high_gravity_zone:
        target_gravity_scale = high_gravity_scale 

    velocity += get_gravity() * target_gravity_scale * delta

Let me know if that works.
Also, please share your code by pasting it as formatted text instead of a screenshot, it’s very tedious having to write it all instead of copying and pasting.

since you are using get_gravity() you should be able to use the Area2D’s gravity combine/replace properties.

1 Like

Holy…moly! I did not know this existed!

@fellowdevloper you should be able to just set the values in your Area2D and whenever the player is in that area the gravity will be double (they will only jump half as high).

Thanks everyone for your Help :smiley:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.