Raycast2D. How to determine the type of object hit, so I can ignore other players

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

Hi all, I’ve had a read here Ray-casting — Godot Engine (3.0) documentation in English 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.

:bust_in_silhouette: Reply From: kidscancode

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.

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

Robster | 2018-02-23 06:38