Knockback in Godot

yudinyudin Posts: 7Member
edited January 13 in Programming

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.

Answers

  • yudinyudin Posts: 7Member

    pls delete this topic. This one makes no sense, I understood that I wrote nonsense.

  • MegalomaniakMegalomaniak Posts: 1,087Admin

    @yudin said:
    pls delete this topic. This one makes no sense, I understood that I wrote nonsense.

    Rather than deleting it, could you write an answer to your OP so you can mark it as solution. There may be others searching for the same thing that can then find this to help them. :) And for this reason there are no stupid questions, BTW. Genuine questions are always valid.

  • yudinyudin Posts: 7Member

    @Megalomaniak said:
    Rather than deleting it, could you write an answer to your OP so you can mark it as solution.

    I will do it the other day. Thanks for the proposition.

Sign In or Register to comment.