RayCast Bulets not colliding

Godot Version

4.6.1

Question

Hi evryone i create an FPS and i got a problem with my RayCast bulets wich rarely collide with walls and ennemies can you help me please

Hey! I’m a 2D dev, but the Godot raycast concepts are the same in 3D. My game uses enemy projectiles and some bullet-hell-style elements, and in an early prototype the player could shoot too, so I’ve had my share of run-ins with this kind of thing.
Quick thing before I dive in: could you be a bit more specific about “rarely collides”? Does it miss at high bullet speeds, miss only certain enemies, miss when you shoot at angles, etc.? That narrows it down a lot.
That said, when a raycast “rarely” hits, it’s almost always one of these:

collision_mask vs collision_layer mix-up
Your raycast’s collision_mask must include the layer your walls and enemies are on. The walls/enemies themselves don’t need a matching mask for the ray to hit them, only their collision_layer matters. By far the most common cause.

  • Every enemy in my game looks like this (base_enemy.gd):
    gdscriptcollision_layer = 4 # enemies live on layer 3 (bit 2 = 4)
    collision_mask = 16 | 1 # they scan for player (layer 5 = 16) and terrain (layer 1 = 1)

So if my bullet is supposed to hit the enemy, the bullet’s collision_mask has to include 4. The enemy itself doesn’t need a matching mask, it just needs to live on layer 3, which it does via collision_layer = 4.

Target has no CollisionShape2D/3D (or it’s disabled)
Check in the Scene Tree that your walls and enemies actually have a valid CollisionShape3D child with a real .shape resource assigned, and that disabled is false.

  • My enemies look like this:
    Enemy (CharacterBody2D, collision_layer = 4)
    ├── CollisionShape2D (shape = RectangleShape2D, disabled = false)
    ├── Sprite
    └── …

Without that CollisionShape, no hit, no matter how the mask is set up.

One more question before I go deeper: are you using a RayCast3D node in the scene, or the scripting API (get_world_3d().direct_space_state with PhysicsRayQueryParameters3D)? Both have their own common gotchas, exclude arrays, collide_with_areas, force_raycast_update, target_position being local, etc. and I can point out the relevant ones once I know which one you’re working with.
If you can paste your bullet-firing snippet and a screenshot of the enemy’s collision layers in the inspector, that would help narrow it down.

4 Likes

thanks for the answer i use the RayCast3D node and all the collisions are activate but i d’ont think it is a collision problem it can maybe from the script or something else

Ok i have found the solution i was not think to activate the collide from inside option so sorry for disturb :downcast_face_with_sweat:

1 Like