I want the gravity of my player to change depending on what level im on

Godot 4
I’m making a 2d platformer that goes to multiple different planets I want the gravity of my player to change based on what planet you are on and I was wondering how I would achieve this. (all the planets are different scenes)`

You could have a variable in your player called gravity,
On your map scene script you could have another variable called world_gravity, set it to what you want the gravity to be, and then, when the map instantiated the player, you could do :

player.gravity = world_gravity 
add_child(player)

you might need to flip the two lines of code, but this should work.

Another thing you could do is have an autoload script that holds map data,

var selected_map : String
var map_gravity : #float or int

func set_gravity():
    match selected_map:
        "world_1":
            map_gravity = #set the gravity
        "world_2":
            map_gravity = #set the gravity
        "world_3":
            map_gravity = #set the gravity

    return map_gravity

Then in the player’s ready function you could do:

func _ready():
    gravity = MapData.set_gravity()

Something like this should work
–EDIT–
Lemme just add, you’ll just have to add this line of code wherever you set your map:

MapData.selected_map = "#map name here"
1 Like

Thank you for the help this would of taken me hours to figure out

1 Like

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