All Questions


Rabbit One

How to ignore some walls?

Hey! What would be the best approach to ignore some walls?

I want my player to slide only over specific walls. I was thinking about creating a raycast in the player scene that detects the layer of the sliding and non sliding walls and enable/disable sliding. Something like the pass-through platforms solution.

But, do you think there is a better approach? Thanks!

  • Nathan Lovato replied

    I think your solution makes sense. Raycasts can give you corner cases (not long enough, not enough rays when hitting a corner...) so I'd work with the player's collider as much as possible. There should be a function that allows you to find what you're colliding with. `move_and_collide` should allow you to find that, although it's not as convenient as using `move_and_slide`.

  • O
    Rabbit One replied

    Sure. Detecting corners using raycasts would be a problem.

    I ended up using this from Godot documentation:

    for i in get_slide_count():
        var collision = get_slide_collision(i)
        if collision.collider.get_collision_layer() == "layer to check"

    I put all sliding walls in a different layer and by using the if statement I could ignore non sliding walls.

    But I have this feeling that using loops to check this kind of information can result in poor performance. I mean, if someday I have multiple colliders in my scene, the game will take too long to detect sliding walls and, when detecting them, it will to be late for the player to slide.

    What do you think? Am I exagerating? Maybe such calculations are too fast to get worried about it. I don't know.

  • Nathan Lovato replied

    It shouldn't be an issue, especially if it's just for the player and in a 2D game. That loop runs at most 5 times in your case, once per frame where there's collisions. It gets some references and compares some numbers.

    If you're worried about performances, in general, run a test with the profiler. You'll see this piece of code will run at most get_slide_count() times per frame.