godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
90.78k stars 21.13k forks source link

Separate collision layers and collision masks for RayCast2D #19219

Closed davidbejlej closed 5 years ago

davidbejlej commented 6 years ago

Godot version: 3.0.2

OS: Windows 10

Sorry if it doesn't fit here, I'll try to keep it short. 2D platformer shooter. I use RayCast2D in bullet scene so I can get collision point (to instance animation in place where bullet the hits) but if I shoot few bullets at once (shotgun weapon) RayCasts detect each other. I set meta for bullet scene and use "has_meta" so bullets don't react to each other, but if they overlap and one of them is being freed (queue_free) I get "Attempt to call function "has_meta" in base "null instance" on a null instance".

Anyway, the point is - I think it would be useful if RayCast2D had separate collision layers and collision masks like Area2D for example.

Steps to reproduce: Bullet scene with RayCast2D hitting KinematicBody2D. Bullets have to overlap other bullets. Bullets need to have meta set to "Bullet" Bullets have to use "has_meta("Bullet") Bullets call queue_free() on KinematicBody2D if colliding.

The project is a mess but maybe it'll help. Just shoot on the enemies on the right using left mouse button and see what happens (the bullets fly very slowly so you can see clearly). Shooter.zip

henriiquecampos commented 6 years ago

Raycasts already have a collision_mask property:

int collision_mask
set_collision_mask(value) setter
get_collision_mask() getter

The ray's collision mask.
Only objects in at least one collision layer enabled in the mask will be detected.
kubecz3k commented 6 years ago

I tried to reproduce the described behavior of raycast vs raycast collision but I was unable to in Godot 3.0.3 rc2. RaycastVsRaycastCollision.zip

KoBeWi commented 5 years ago

The reason RaycastShapes2D don't have collision layer is that they only detect collision, not can be collided with. So the bug in original project either was fixed (as tested by kubecz3k) or was caused by something else.

akien-mga commented 5 years ago

Thanks, closing then.