Center my character in a tilemap

Godot Version

Godot 4.3

Question

Hey everyone. I am making a 2d platformer game, and I want it to center to the nearest tile when pressing a key, but let the character move freely when not. Has anyone did this or could help me? Appreciate it

I mean, you can make use of your tilemaps’s specifics for example a tilesize of Vector2(x,y).
When you press on the key, your character’s global position is divided with the tilesize and then times the tilesize to get rid of the remainder

var tileIndexes = character.global_position / your_tilesize
character.global_position = tileIndexes * your_tilesize

or at least that’s the logic.