Raycast on TileMap Returns Different Coordinates Based on Hit Direction

Godot Version

4.2

Question

Hi everyone,

I am having an issue with using raycast on a tilemap in Godot (version [your Godot version]). When the raycast hits a tile from the left, the coordinates returned are different from those when the raycast hits the same tile from the right.

Here is a summary of the problem:

  • I am using a TileMap to draw stairs, which has a physical collider with the oneWay property enabled.
  • When a raycast hits a tile from the left, I get specific X and Y coordinates.
  • However, when the raycast hits the same tile from the right, the coordinates returned are different.
  • My goal is to move the character to the center of the stairs when a button is pressed, but this only works if the character is to the left of the stairs.

I have included some GIFs showing the problem and the code I am using. This is a new project I created to demonstrate the issue and it contains nothing else.

func move_to_center_of_ladder():
    if ray.is_colliding():
        var collider = ray.get_collider()
        if collider is TileMap:
            var collisionPoint = ray.get_collision_point()
            var local_to_map = collider.local_to_map(collisionPoint)
            print("local to map = ", local_to_map)
            var map_to_local = collider.map_to_local(local_to_map)
            print("map to local = ", map_to_local)
            var center = map_to_local.x
            
            global_position.x = center

I have tried adjusting the TileMap settings and reviewing the raycast code, but I have not been able to resolve the issue. Has anyone experienced something similar or have any ideas on why this might be happening?

I appreciate any help you can offer.

RaycastTileTest1
RaycastTileTest2