Have a problem with a area2d not detecting a another hitbox

Godot Version

func _on_hurt_box_area_entered(area):
	print("something got in")

	if area.has_method("Sword"):
		print("entered")
		player_in_attack_zone = true
		print("test", player_in_attack_zone)
		
func _on_hurt_box_area_exited(area):
	print("something got out")
	if area.has_method("Sword"):
		print("exit")
		player_in_attack_zone = false
		print(player_in_attack_zone)

Question

I got this code in my enemy that is supposed to detect my sword which has a hitbox.


but for some reason the sword is not getting detected. the area 2d is detecting areas and I know this because it does detect the player hitbox entering but the sword does not.

What are the values for collision_layer and collision_mask in your enemy scene? How is your enemy scene structured? Is any of the print statements from the code above printed? If so, which ones? What Godot version are you using?

im using godot 4

extends CharacterBody2D
class_name Enemy

#for death animation
@export var deathparticle : PackedScene

#var for combat
var health = 60
var player_in_attack_zone = false

func _physics_process(delta):
	#the code for damage from combat

	Damaged()
	#movment code this works because of the statemachines
	move_and_slide()
	#animation code. 
	if velocity.length() > 0:
		$AnimatedSprite2D.play("RUN")
	
	#flips the sprite if the velocity caluclated in the states are a certain value
	if velocity.x > 0:
		$AnimatedSprite2D.flip_h = false
	else:
		$AnimatedSprite2D.flip_h = true

#for combat code in player
func Enemy():
	pass
> func _on_hurt_box_area_entered(area):
> 	print("something got in")
> 
> 	if area.has_method("Sword"):
> 		print("entered")
> 		player_in_attack_zone = true
> 		print("test", player_in_attack_zone)
> 		
> func _on_hurt_box_area_exited(area):
> 	print("something got out")
> 	if area.has_method("Sword"):
> 		print("exit")
> 		player_in_attack_zone = false
> 		print(player_in_attack_zone)


#if the sword with the swing function enters the hurtbox turn on can attack
#func _on_hurt_box_body_entered(body):
	#if body.has_method("Sword"):
		#print("entered")
		#player_in_attack_zone = true
		#
#
#
#func _on_hurt_box_body_exited(body):
	#if body.has_method("Sword"):
		#print("exit")
		#player_in_attack_zone = false

func Damaged():
	if player_in_attack_zone and global.player_current_attack == true:
		print("damaged", health)
		health = health - 20
		if health <= 0:
			kill()
#code for when killed
func kill():
	var _particle = deathparticle.instantiate()
	_particle.position = global_position
	_particle.rotation = global_rotation
	_particle.emitting = true
	get_tree().current_scene.add_child(_particle)
	
	queue_free()

enemy code. the quoted area is the code to detect the sword
the collision layer and mask are all on the same layer 1
the sword is a sprite that goes around the player with a hitbox. but this hitbox is not getting detected

when the player get close the print entered and exited gets printed but not the bottom part

According to the code you shared in the first post, the HitBox of your Sword has different values:

func _init() -> void:
    collision_layer = 2
    collision_mask = 0

An Area2D will only detect other areas if they are on at least one collision_layer that is enabled in its collision_mask. However, in your case, the collsion_mask is set to 0, so no layers are enabled.

Worked thank you

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