0 votes

Hi all, I've had a read here https://godot.readthedocs.io/en/3.0/tutorials/physics/ray-casting.html#collision-exceptions but it's just not clicking with me.

I have a kinematicBody2D as my character. There are multi characters on screen. When they fall, they use a raycast to see what's under them and when it has a collision it changes state from fall to walk.

Unfortunately if it hits another player it walks on his head and not the ground. How can I have them ignore other players?

Thank you.

in Engine by (835 points)

1 Answer

+4 votes
Best answer

You can add exceptions, but the problem there is you have to add every body that you want the ray to ignore, and this can get cumbersome.

The correct answer to this problem is to use the collision layer system. Put the ground/obstacles on one layer and the player(s) on another. Set the raycast's mask to only scan the obstacle layer.

Note that in 3.0, you can assign names to each layer in Project Settings, which makes setting these up a lot more convenient/easier.

That said, if you're using the Physics2DDirectSpaceState.intersect_ray() method rather than a RayCast2D node attached to your character, then you can't use the names but must pass the method a bitmask for the layers you want it to scan. I don't know the details of your implementation, but it sounds like the node-based solution might be sufficient if all you're using it for is ground-detection.

by (21,977 points)
selected by

Such a beginners mistake. Thank you! Collision layers it is

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 Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.