I'm losing my head trying to give knockback to enemies

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By collello7

My problem is simple: I want to give knockback to enemies. I initially used HeartBeast’s as a tutorial and adapted the deprecated or changed things in Godot 4.0 and for the most part, I’m succeeding. I even created some functions on my own (like temporary invincibility on hit and roll)
But when I tried to implement the knockback, no amount of tutorials and guides gave me the information I needed.

The point I am now after watching a youtuber called Bitlytic is this:

func _on_hitbox_area_entered(area, attack_damage, knockback_force, attack_position):
stats.health -= attack_damage
velocity = (global_position - attack_position).normalized()*knockback_force
if stats.health <= 0:

This func doesn’t work because the program call only the first argument.


extends Area2D
var attack_damage := 2
var knockback_force := 100
func _on_hitbox_area_entered(area):
if area.has_method("damage"):
area.damage(attack_damage, knockback_force, global_position)

What I initially tried to do was this:

func _on_hitbox_area_entered(area):
stats.health -= 1
velocity = (global_position - attack_position).normalized()
if stats.health <= 0:

Obviously “velocity = (global_position - attack_position).normalized()” did not work.

If someone could at least teach me in general how to set up the knockback and the way it works, I would be very grateful.

:bust_in_silhouette: Reply From: VTVL

I’m new to the Q&A, but I will have a go at this:

From what I understand, you are failing on the signal call on the function in the enemy script.
In the first enemy code excerpt, you may instead of a signal just name the function damage() with the parameters given. That should help, from what I understand, having the attacker call the enemy function with information on the attack itself.
If you are having an issue into the knockback calculation, could you provide more info on whats going wrong with the calculation?