All you really need to do is get the motion vector of your bullet, invert it (that is, negate all the components so it’s reversed) normalize it (so the vector is length 1), scale it by how much knockback you want, and apply that as an impulse to the player.
You should be able to do that all in shoot().
For future reference, code formats more nicely here on the forums if you do:
Assuming it’s top-down, you could do something like:
# in the gun code
const KNOCKBACK_SCALE: float = 100.0 # Probably not the right value, tune this...
func shoot() -> void:
[...stuff...]
# Make a vector that's a unit vector rotated as the bullet is, plus another
# 180 degrees so it's heading in the opposite direction. We do that by adding
# PI, since there are 2*PI radians in a circle, so PI is half a circle, in
# radians.
var knockback: Vector2 = Vector2.RIGHT.rotated(rotation + PI)
# Add the knockback to the player's velocity as an impulse, scaled by the
# amount you want to kick the player.
player.velocity += knockback * KNOCKBACK_SCALE
You’ll probably have to wire up access to the player from the gun.
Or something like that. It depends on where the gun and the player are relative to each other in the node hierarchy. If the gun is a child of the player, get_parent() or $".." will work. If they’re further apart, you’ll need the right node path.
Change the value of KNOCKBACK_SCALE; a smaller number means less knockback, a bigger number means more.
What’s going on here is we’re building a vector, knockback, which contains the direction we want the knockback to go in. We’re making that length 1.0 (that is, the length of the line from the origin to the vector (as a position) is a distance of 1.0).
We then change the amount of force by multiplying the knockback vector by KNOCKBACK_SCALE, which changes the length of the vector to be equal to that value, so if KNOCKBACK_SCALE was 5.0, the length of knockback will become 5.0.