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:
Enemy:
func _on_hitbox_area_entered(area, attack_damage, knockback_force, attack_position):
stats.health -= attack_damage
print(stats.health)
velocity = (global_position - attack_position).normalized()*knockback_force
if stats.health <= 0:
queue_free()
This func doesn't work because the program call only the first argument.
Hitbox:
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:
Enemy:
func _on_hitbox_area_entered(area):
stats.health -= 1
print(stats.health)
velocity = (global_position - attack_position).normalized()
if stats.health <= 0:
queue_free()
Obviously "velocity = (globalposition - attackposition).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.