Hi, I'm trying to have melee attacks in my top down shooter, and I want to have some knockback when you hit an enemy. There aren't any tutorials online, and I've tried having move_and_slide() and others to try and create the image on knockback, but it isn't working. Help would be appreciated!
Adding knockback to my top down game.
Hi UnrulyElf10, here is a good tutorial series, where I learned how to make blood particles, but other then that the guy shows and knock back logic in this one:
@gargara this tutorial isn't the best since the enemy is pushed back based on its own vector. In an instance where the enemy was moving away from the player, on the "hitbox entered" event, the enemy would be pushed towards the player and the player would lose health
- Edited
I recommend using this system: enemy_movement_vector = playerbullet.global_position.direction_to(enemy.global_position) * knockback_speed
- Edited
var hitstun = 0
var velocity = Vector2.ZERO
var knockback= Vector2.ZERO
var knockbackSpeed = 110
onready var hurtbox = $Hurtbox
func _physics_process(delta):
move()
if hitstun > 0:
hitstun -= 1
elif hit_stun == 0 && hurtbox.monitoring == false:
hurtbox.set_deferred("monitoring", true)
func move():
if hitstun == 0:
velocity = move_and_slide(velocity)
else:
knockback= move_and_slide(knockback)
func _on_Hurtbox_area_entered(area):
hitstun = 10
health -= area.damage # I added an export var damage = 1 on the "hitbox"
knockback = global_position - area.global_position
knockback = (knockback.normalized() * knockbackSpeed)
hurtbox.set_deferred("monitoring", false)
create an area2D call it what ever you call the area_entered signal in this case Hurtbox make it have its own collition layer and a collision shape. then make the hitbox of whatever attack or enemy collition mask to that layer.
you can also add something like the monitoring stuff to make it so that u cant get hit whilst "stunned/knockbacked" if not wanted just ignore spoilers. altho not the best way to do "invincibility" but it is an easy way to do it on a comment x)
Is that formatting actually intentional or might you need help formatting that post?
I tired. xD
Alright you had mixed spaces and tabs in your indentation/in your code and you had entered some random spoilers at places that didn't make much sense but other than that there weren't any real issues there.
Theres a topic here you can read to better understand post formatting: /discussion/27022/getting-started-on-the-forums