© 2023 by Andrew Jonhardt. Proudly created with Wix.com

  • Andrew Jonhardt

Godot 2D Walljumping part 1: Setup

As usual, little in the past few weeks has gone as expected. I originally planned to make this article about pathfinding, and I got as far as getting a tileset in a scene to self-report the locations for each tile:

var tile_repac = []


func _ready():

for i in self.get_used_cells().size():

var tiles = self.map_to_world(get_used_cells()[i])

tile_repac.append(tiles)

if tile_repac.size() ==self.get_used_cells().size():

Global.CurrentMap = tile_repac


The above script, when placed on a Tilemap node containing a loaded tileset, uses the get_used_cells() function to cycle through the Tilemap and load all tiles in a scene into the tile_repac variable. The last part of the script passes the contents of tile_repac to a variable, called CurrentMap, in a separate script, called Global.


I struggled so much to get any further that my still-healing vision was impacted (still got 2 months left, oi). So, I decided to try something I assumed would be easier; I decided to make a platformer instead.


I never before considered making a basic platformer. Unless the protagonist has a ranged attack, and the game is more Metroidvania than Mario, I'll happily steer clear. So, you can probably understand my surprise when a stray comment in a random YouTube video got me thinking about walljumping.


I'll avoid delving too far into my thought processes, as neither the video nor the route my tired mind took to get to "I should make a platformer" were particularly special. Suffice to say, something clicked on in my head, and a whole host of ideas tumbled out.


A basic script for controlling a 2D character in Godot is terribly easy to find. The people behind Godot provide about 4 2D movement scripts themselves. I tried all the basic provided scripts, and settled on the one found in this article:

https://docs.godotengine.org/en/3.1/tutorials/physics/using_kinematic_body_2d.html


The next step after movement was jumping, which turned out to be a simple matter of always applying a downforce to the player character's velocity. This took some getting used to, as I previously thought only of applying downforces to objects themselves. Instead, the following line only applies a gravity force to the variable that impacts movement:

velocity.y += gravity * delta


Put a different way: I'm used to gravity directly impacting an object's position, rather than indirectly through the variable that controls over all movement. I'm not sure if that it explains it any clearer, so I'll just move on.


With a form of gravity in place, all you need is a way to temporarily push against said gravity:

if jump and on_floor:

velocity.y = -jump_speed * delta


Despite my dislike of Mario, I've grown partial to variable-height jumps. Or, jumps that allow you to go up higher if you hold your jump button down longer. So, I searched around until I found a way to set a minimum jump height to reproduce the effect of not holding the button down long enough to reach max height:

if jump_release and !on_wall:

velocity.y = clamp(velocity.y, -100, velocity.y)


In the above example, if the player lets go of the jump button (jump_release) and is not on a wall, the velocity.y (jump-impacting value) is set to a low number. Which, brings us to wall-jumping mechanics.


I'm going to try for a Thursday blog post next week, as I've still got alot to cover. I'm just out of time for tonight.


Until next week.