+1 vote

As the title suggests, in my project, I'm using two booleans, one when the weapon is inside the enemy, and another for if the attack animation is active. If both are active, then I can damage the enemy. The issue I'm encountering is that the weapon only damages the enemy when the hilt (the origin of the area 2d) is touching it. When the blade, which is a part of the same collision shape, touches the enemy, nothing happens. In the image attached, I've set up strings for testing. I attacked away from the enemy twice, twice when I attacked the enemy with the blade, and another two for when I attacked the enemy up close with the hilt. It clearly shows that when the blade touches the enemy, both conditions are met, but it doesn't trigger for some reason. Any help would be appreciated! Attached is the code I'm using and the results below it.
Link to the image if the embed doesnt work https://ibb.co/k9NywfQ
The code

Godot version 3.2.3
in Engine by (105 points)
edited by

It was a good idea, but it didn't work. I'm not sure what to do anymore :/ Thanks for the help, and for your time, I really appreciate it

But like, the more it doesn't work, the more I'm intrigued how it's possible that it doesn't work and then more interested I get.

But I think I might have overlooked something pretty major as well:
All of this is all contained within is_action_just_pressed and the code won't (re)evaluate unless the action is just pressed.

Thus: try mashing the attack button really fast: does it...?

And one more thing if you decide to come back to this: put is_in_body = false inside of the if is_attacking == true and is_in_body == true: and delete that other function.

Good luck!

You're an absolute genius; putting the isinbody = false functioning inside the if statement worked! I can't believe I didn't think of that. Thank you so much for your help and advice, it was really helpful. Can't thank you enough.

Haha... I don't know about genius... it took me this much just to debug < 40 lines of code with you. But I'm really really glad you got it working! I'll simplify this thread into an answer so the question can be seen as solved.

Great, thanks.

1 Answer

+2 votes
Best answer

Overview:

As per the full code when the Area2D enters the enemy body it signals:

func on_Sword_body_entered(body):
    if body.name == "PlayerTwo":
       is_in_body = true
       print(str(isinbody, "inbody"))

Which sets the is_in_body bool to true and prints this information (giving a false positive diagnostic), but another function is signaled when the Area2D "exits" — and for whatever reason this signal is emitted immediately:

func on_Sword_body_exited(body):
    if body.name == "PlayerTwo":
        is_in_body = false

Thus by the time _physics_process starts processing its code the is_in_body bool has already been set to false.

func _physics_process(delta):
    if Input.is_action_just_pressed("attack") and can_attack == true:
        $AnimationPlayer.play("swing")
        can_attack = false
        attacktimer.start()
        swingtimer.start()
        if $AnimationPlayer.currentanimation == "swing":
            is_attacking = true
            print(str(isattacking, "attacking"))
            if is_attacking == true and is_in_body == true:
                print("attacksuccessful")
                get_node("/root/TestLevel/CanvasLayer/FightUI/Player2Health").value -= 20

Solution:

The end solution as we finally found was to put the is_in_body = false into the final attacking branch: if is_attacking == true, and delete the _body_exited signal.

This made it so the player couldn't spam multiple attacks if they hit whilst also fixing the issue at hand. Final revision:

if is_attacking == true and is_in_body == true:
    is_in_body = false
    print("attacksuccessful")
    get_node("/root/TestLevel/CanvasLayer/FightUI/Player2Health").value -= 20

Actual Issue:

Unknown — but it's possible that the incorrect triggering of the exit signal function is an engine bug, or perhaps the circular CollisionShapes of the Sword and the Enemy combined is causing the geometry to somehow register as both entering and exiting simultaneously.

If you need to use the _body_exited collider signal for something else, try using rectangular or polygon shapes instead of circles.

Anyway, good luck and have fun with the rest of your project!

by (1,014 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.