Hello, guys. I stuck with some problem trying to realize knockback effect. I wrote/found some code and I get kind of unexpected behavior for me. If I jump on the enemy under a certain degree, it knockbacks my character in unexpected way. I can get boosted far away into the sky. And if I jump on the top of the enemy, knockback doesn't even work. If I just come to enemy walking without jumping, it works almost correctly. I guess, you can see what I tried to describe if you put my code into your ide. Nevermind on var TYPE, it doesn't matter in current question.
const TYPE = "ENEMY" const SPEED = 0 const VTOP = Vector2(0, -1) var grav = 2000 var jump = 35000 var hitstun = 0 var vel = Vector2() var knockDir = Vector2(0,0) func damage_loop(): if hitstun > 0: hitstun -=1 for body in $hitbox.get_overlapping_bodies(): if hitstun == 0 and body.get("DAMAGE") != null and body.get("TYPE") != TYPE: hitstun = 10 knockDir = (transform.origin - body.transform.origin) * 12 func movement_loop(delta): if Input.is_action_pressed('ui_left'): vel.x = -SPEED * delta elif Input.is_action_pressed('ui_right'): vel.x = SPEED * delta else: vel.x = 0 * delta vel.y += grav * delta if is_on_floor() && Input.is_action_just_pressed('ui_up'): vel.y = -jump * delta if hitstun == 0: vel = move_and_slide(vel,VTOP) else: vel = move_and_slide(knockDir, VTOP)
Im sorry for putting a code like an idiot, dont know how to make it works correctly. I hope, you can help me with it. Thank you.