Raycast2D doesn't collide with TileMap

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By wndr

Hey. I have a set of 4 Raycasts coming out of my player’s body. They are all enabled and have their collision mask bit 0 set to true. I also have a TileMap with the same collision mask and layer bit set to true (bit 0). However, while my player collides with the TileMap properly, my raycasts don’t.

Raycast code:

func _check_is_valid_wall(raycasts):
    for raycast in raycasts.get_children():
        if raycast.is_colliding():
            var angle = acos(Vector2.UP.dot(raycast.get_collision_normal()))
            if dot > PI * 0.35 && PI * 0.55:
                return true
    return false

Additionally, on the TileMap, use_parent and use_kinematic are set to false, friction is set to 1 and bounce set to 0. I made the collision shapes by selecting a region of the tilesheet with New Single Tile, followed by creating a new collision rectangle. Every other value in the tile is set to their default.

EDIT:
I have a very strong feeling that this behavior happens because of how the engine makes the collision polys for the tiles. It’s actually just very thin (0-thickness) lines; For the purposes of one way collision and such. I believe that because of this, raycasts can pass through these walls if cast from close enough. Not sure though.

I’m also having an issue with raycast2d and tilesets

the get_children() method is throwing the error “Invalid call. Nonexistent function ‘get_children’ in base ‘Nil’”

is this true for you aswell?

rodan | 2020-03-06 13:56

No, I get no errors.

wndr | 2020-03-06 14:35

I fixed my earlier issue and am now experiencing the same thing you are. I’ll let you know if I figure anything out

rodan | 2020-03-06 15:24

Thanks. I’ll keep this thread updated with anything I find.

wndr | 2020-03-06 15:45

:bust_in_silhouette: Reply From: grey_lotus

Have you ensured the RayCast2D enabled property is set to true?
You can try using

raycast.castto()
raycast.getcollider()
print (collider.name)

methods to check if the ray is actually not colliding with the tile shapes

Yes. My raycasts collide properly with other things around the scene, so they are enabled

wndr | 2020-03-06 10:21

Update: I tried to use getCollider() to find out if the rays actually collided with the wall. Turns out they do, but for only one frame, then they’re counted as not actually colliding anymore.

For reference, this is I figured this out by putting the following code in physicsProcess

var collider = raycast.getCollider()
print (collider)

this prints null objects while we’re not near any walls, prints the tile for one frame after we touch it, then continues printing null.

For reference, when I tested this against StaticBodies2Ds I have setup, it constantly printed its collider.

Still have no idea why they don’t detect the collision every frame though.

wndr | 2020-03-06 21:57

:bust_in_silhouette: Reply From: rodan

I’ve been messing around with it and I’ve noticed that making the raycasts longer (I had mine set to 3) has fixed the issue. Using get_collider() I can see that the rays successfully make contact with the tilemap. Before get_collider() was only returning the tilemap ID on the first impact, and then nothing, but making them longer and repositioning them a bit has resolved that. Unfortunately making them bigger and moving them out of the way doesn’t fix all my issues, but hopefully it helps you. I wonder why the collision is so strange at smaller raycast sizes…