I'm new to working in Godot, and this is something I've been trying to figure out for a bit now. I searched for solutions, but couldn't find anything useful.

To put it simply, I have ground type tiles and water type tiles. I want the character animation to change when the character is standing on water tiles.

I've given the water tiles an "is_water" custom data layer, and then in the Player code, I check whether the player is standing on a water tile, and change the animation based on that. It does work, however there is a slight sprite changing delay when the character steps in and out of water (so the water animation still plays for a couple of frames when the player steps onto the ground, and the ground animation still plays for a bit when the player steps into the water).

My question is whether is possible to somehow get rid of the delay, or maybe do this process in a different way?

The relevant code:

func _physics_process(delta):	
	var forward_animation : String = "forward_run"
	var back_animation : String = "back_run"
	var right_animation : String = "right_run"
	var left_animation : String = "left_run"
	
	var tilemap = get_tree().get_current_scene().get_node("TileMap")
	var tile_data = tilemap.get_cell_tile_data(0, tilemap.local_to_map(global_position))
	
	if (tile_data.get_custom_data("is_water")):
		forward_animation = "water_" + forward_animation
		back_animation = "water_" + back_animation
		right_animation = "water_" + right_animation
		left_animation = "water_" + left_animation 
		run_speed = water_run_speed

Ok, I had a good night's sleep and figured it out by myself. The problem was the global position grabs the coordinates of the center of the sprite, which was the reason for the delay in the change of animation. All I had to do was apply a shift:

var tile_data = tilemap.get_cell_tile_data(0, tilemap.local_to_map(Vector2(global_position.x, global_position.y + 18)))

Now it works great. I'll leave this up in case someone runs into the same problem.