Godot Version
Godot 4.5
Question
So I’m making an enemy for my platformer and the hitbox that is supposed to kill the enemy if the player jumps on it. I’ve coded it to do this, but when I jump on it I just stand on it and I move with it. How do I fix this? Here’s the code in the enemy:
``extends CharacterBody2D
@onready var animsprite = $AnimatedSprite2D
@onready var sidecast = $SideCastLeft
@onready var downcast = $BottomCastLeft
const grav = 2250
var speed = 200
var faceright = false
func _physics_process(delta: float) → void:
gravity(delta)
move()
move_and_slide()
if sidecast.is_colliding():
flip()
if not downcast.is_colliding():
flip()
func gravity(delta):
if not is_on_floor():
velocity.y += grav * delta
func move():
velocity.x = speed
animsprite.play(“default”)
func flip():
faceright = !faceright
scale.x = abs(scale.x) * -1
if faceright:
speed = abs(speed)
else:
speed = abs(speed) * -1
func _on_hitbox_body_entered(body):
if body.is_in_group(“Player”):
die()
func die():
queue_free()``
Hi,
I’d suggest you debug your code a little bit. You can do that by adding print calls, like this:
func _on_hitbox_body_entered(body):
print("Hitbox entered!")
if body.is_in_group(“Player”):
print("Hit by player!")
die()
This will give you more info about what’s happening:
1/ If none of the prints is showing up, then that means there’s no collision detection at all. In such a case, you may want to look at your collision layers, or make sure the function is properly connected to the signal.
2/ If the first print shows up but not the second one, that means there’s a collision detection, but that the body does not belong to the “Player” group.
Let me know about what you find.
1 Like
Sadly none of the prints show up, and the player is in the Player group with the code func _ready(): add_to_group("Player")
I think it is a problem with the actual hitbox but every collision is layer 1 and mask 1 so I’m not sure what is wrong with it. I’ve also tried extending the player’s hitbox, but I’ve only gotten the same result
1 Like
Check that the signal is properly connected.
2 Likes
I did not know connecting it was required woops
It is. How else would the signal know which function to call? Signals are just function calls.
1 Like
Yeah I realize that now, thanks
1 Like