Hello,

i would need abit of Help on Knockback. In a 2D Plattformer

I dont know how to know from wich direction the bullet is comming that enters the Enemy Hitbox/or On wich Side from the enemy the Player is standing. Is there a Way? Otherwise I don't know if the knockback on x oder -x must be. I want the knockback happen away from the player.

Welcome to the forums @TrippleSeven!

How are you detecting when the bullet hits something and which nodes are you using? If you know which nodes you have during the collision, you can do a simple position compare to get a rough idea of which direction to use for knockback (untested):

# We'll assume "player" is the thing being hit by the
# bullet and "bullet" is the bullet. The code also assumes
# that both are variables referencing Node2D-based nodes.

var knockback_strength = 64
var player_to_bullet = player.global_position.direction_to(bullet.global_position)
var knockback = player_to_bullet * knockback_strength
player.velocity -= knockback

However, with this solution, it will not take into account the collision shape of the bullet or whatever it hits, just the direction from the origin of the bullet to the origin of the other entity. If you need to take the collision shape into account to get a normal vector, I would suggest using a 2D raycast (documentation).

@TwistedTwigleg First of all thank you for the awnser and the welcoming :)

Im pretty new to godot and programming, just learing it for now about for 3 weeks.

I have a simple code with several .tscn

1 is for Hitbox.tscn 1 is for Hurtbox.tscn 1 is for Player.tscn 1 is for Enemy.tscn

When Player shoot bullet, I am Detecting by the attached hurtbox to enemy is hitted

Hittbox.tscn is a a Aread2d with a collisionshape2d:

 extends Area2D

export(int) var damage = 1

func _on_Hitbox_area_entered(hurtbox):
hurtbox.emit_signal("hit", damage)

Hurtbox is aswell a Aread2d with a collisionshape2d

extends Area2D

# warning-ignore:unused_signal
signal hit(damage)

in the enemy i have bouth hitbox and hurtbox attached. So i can do this func

extends KinematicBody2D

func _on_Hurtbox_hit(damage):
	stats.health -= damage

I am working with preset hitbox and hurtbox, aswell with a Stats preset, so i can just drag and drop these into the kinematicbody when i create a new enemy.

My problem is now, i dont know how to read out the position from the player to have the position from where he is shooting or know the direction the bullet that enters the hitbox. So i can knockback in the other direction.

I hope there is a simple solution for that.

Best Regards

Here’s what I would try, based on the scripts provided. For the signal, I would define it like this so you can pass a position:

signal hit(damage, hit_position)

Then, when emitting the signal, I’d pass the global position, since that gives you the most flexibility if you need to use the position for something else, and you can get the direction using the global positions:

hurtbox.emit(“hit”, damage, global_position)

And then, in your _on_Hurtbox_hit function, I would get the direction and push the player accordingly:

const KNOCKBACK_STRENGTH = 64

func _on_Hurtbox_hit(damage, hit_position):
	var hit_direction = global_position.direction_to(hit_position)
	velocity -= hit_direction * KNOCKBACK_STRENGTH
	
	stats.health -= damage

If you wanted, you could also pass the knockback strength in a way similar to how you pass the position, if you wanted to have varying knockback strengths. Also, the code above assumes you have a variable called velocity that you are using to move the player, using code similar to the 2D movement section in the Godot documentation.

Thank you @TwistedTwigleg alot for the Help. I am going to try this out <3

@TwistedTwigleg Hello again, I have try the code, but the Knockback does not work, and if I implement this code then even the damage on the player when he does enter the body from enemy does not work. Any sugestions?

@TrippleSeven said: @TwistedTwigleg Hello again, I have try the code, but the Knockback does not work, and if I implement this code then even the damage on the player when he does enter the body from enemy does not work. Any sugestions?

How are you connecting the signal? I’d look there first. I’d also suggest adding some print statements after emitting the signal and in the function that receives the signal, to double check that both parts of the code are being called.

@TwistedTwigleg Thank you for the help, i got it to work now.

Another question: I have a weird bug, when i shoot the enemy from one side. Then jump over and hit it from the other side, the first 2 shots knockback in the same way the first shoot did, after those 2 times it knockback in the right way.

@TrippleSeven said: @TwistedTwigleg Thank you for the help, i got it to work now.

Great!

Another question: I have a weird bug, when i shoot the enemy from one side. Then jump over and hit it from the other side, the first 2 shots knockback in the same way the first shoot did, after those 2 times it knockback in the right way.

Hmm, I'm not sure right off. Maybe the hit_direction passed for the second shot isn't correct? Its strange that it fixes itself after two times though.

2 years later