when changing collision layer in code it doesn't collide with anyhting

4.3 Godot Version

problem
I’m making a top-down game, and I want the player to be able to hit a projectile back at the enemy, but when I set the collision layer to the player hitbox, the enemy hurtbox won’t detect it.

This is the code
Layer 11 is the player’s hit box
Layer 4 is the enemy’s hurt box

func _on_hurt_box_area_entered(_area: Area2D) → void:
collisionBox.set_deferred(“disabled”, true)
hurtBox.create_hit_effect()
speed = speed * -1
hitBox.collision_layer = 11
hitBox.collision_mask = 4
collisionBox.set_deferred(“disabled”, false)

collision_layer and collision_mask are not just integer values, they represent a binary mask. The value 4 sets the bitmask to only bit 3, and 11 to bits 4, 2, and 1.

hitBox.collision_layer = 11 (0b1011)
hitBox.collision_mask  =  4 (0b0100)

Maybe you should use hitBox.set_collision_layer_value() and hitBox.set_collision_mask_value()

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.