0 votes

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, useparent and usekinematic 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.

asked Mar 6 in Engine by wndr (22 points)
edited Mar 7 by wndr

I'm also having an issue with raycast2d and tilesets

the getchildren() method is throwing the error "Invalid call. Nonexistent function 'getchildren' in base 'Nil'"

is this true for you aswell?

No, I get no errors.

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

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

2 Answers

0 votes

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

answered Mar 6 by grey_lotus (25 points)

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

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.

0 votes

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..

answered Mar 7 by rodan (19 points)
edited Mar 7 by rodan
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.