+1 vote

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 = (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.

Godot version 4.0
in Engine by (13 points)
edited by

1 Answer

0 votes

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?

by (50 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.