0 votes

So I followed a tutorial for making a knockback system, the first one that pops up on youtube, fiddled around with it a bunch but made it to work, but when I copy paste an enemy and try to knockback the other enemy it just knockbacks the original instance. I genuinelly have no idea why this happens, there is a comment that has the same exact issue, here is the code:

func punch_knockback():
    if knockback == true and is_on_floor():
        if facingright != player_dir:
            facingright = player_dir
            scale.x = -scale.x
    if knockback == true:
        if facingright == true:
            motion.x = 500
        elif facingright == false:
            motion.x = -500
    if knockback == true:
        motion.y = -100
            if $Sprite/detectwallbounceright.is_colliding() and facingright == true and bouncetimer > 0.1:
            motion.x = -60
            facingright = false
            bouncetimer = 0
            scale.x = -1
            Global.camera.shake(0.1,0.5)
            frameFreeze(0.01, 0.2)
        elif $Sprite/detectwallbounceright.is_colliding() and facingright == false and bouncetimer > 0.1:
            motion.x = 60
            facingright = true
            bouncetimer = 0
            scale.x = -1
            Global.camera.shake(0.1,0.5)
            frameFreeze(0.01, 0.2)

        motion.x = clamp(motion.x,-knockmaxspeedx,knockmaxspeedx)
        if knocktimer > maxknocktime:
            knockback = false

here is the code from the player:

    for body in $attack.get_overlapping_bodies():
    if knockback_wait <= 0 and body.get("NAME") == "enemy" and punchslide == true:
        knockback_wait = 50
        emit_signal("knockback")
        frameFreeze(0.01, 1.0)
        Global.camera.shake(0.2,2)

Thank you all in advance

Godot version 3.5.1
in Engine by (12 points)

1 Answer

0 votes

You should add the video URL and the comment so the community can help faster. Anyway, I watched the video and saw a problem, you need to pass the enemy body using the signal, then you are already fixing the bug of all enemies getting effected.
Also make sure to connect all the enemies, so you can get rid of the "only first instance is affected".

Player script:

signal knockback(enemy_body)

...

for body in $attack.get_overlapping_bodies():
    if knockback_wait <= 0 and body.get("NAME") == "enemy" and punchslide == true:
        knockback_wait = 50
        emit_signal("knockback", body) #pass the body that is on Area2D

Enemy script:

func _on_player_knockback(body):
    if body != self: #cancel if body isn't this enemy
        return
    // rest of the code

The tutorial: youtube.com/watch?v=OOnI8LUDXz0

by (14 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.