That displacement is issued by the physics engine when two CollisionShapes on the same layer overlap - because, well, it's the physics engine's job to prevent that. I'm not aware of a way to influence the direction of that displacement though.
So if you're unhappy with the default displacement, you'll need to prevent the collsion in the first place and have to do the displacement yourself. Here's a sketch:
func set_cell(cell_coordinates, tile_id):
var cell_origin = $TileMap.map_to_world(cell_coordinates)
var cell_size = $TileMap.cell_size
if Rect2(cell_origin, cell_size).has_point($Player.position):
$Player.position.y -= $TileMap.cell_size.y
This checks if the area of the cell contains the current position of the player node and if that's the case it moves the player one cell up before it changes the tile.
Now obviously there are certain shortcoming here like...
- it only checks for the player, but maybe you want to check for other objects (NPCs, enemies, items, etc.) as well. In that case you could move an Area2D to the cells position and use
get_overlapping_bodies(). However, that requires you to wait for the next physics update, so you've to yield for 1 or 2 frames.
- it only checks if the player's position is inside the cell, but even if that's not the case, parts of the player could still be inside the cell (depending on how your movement works, with a grid-based system that wouldn't happen of course!). You could solve this by using Rect2's
encloses-method and calculating exactly which rectangular area the player covers. Or you check all the outmost points of the player individually for more complex, non-rectangular player shapes.
But I feel like it's hard to come up with a general answer to all those problems.
Btw: asking the same question over and over again just slightly paraphrased will not yield better or quicker answers - it just annoys the fuck out of everyone! However, investing the time to ask a well-structured question that includes what you've already tried, why it didn't work or fit your requirements as well as everything else you think could make the life easier for anyone trying to help you out (voluntarily, in their free time, on an unpaid basis) will increase the odds of an answer significantly!
For starters you could provide people with a copy of your project, so they don't have to replicate your setup first. Or at least you could give some details about the size of your cells and the player, how your scene tree looks or the movement of your player works. The less information there is, the less useful the answer will be as well!