Area2D body entered but never exit

dealchilldealchill Posts: 6Member
edited March 25 in Programming

Hi guys,

So I'm trying to make my character take damage when entering an area2d node, but how do I keep it constantly trigger even if my character doesn't leave the area2d?

If happens only once but then it feels like I'm riding the wolf now XD
There's 2 CollisionShape2D that I use for the wolf, one for the KinematicBody and the other for Area2D
I also made the CollisionShape2D for the Area2D slightly bigger.

Here the coding I use:

For wolf:

func _on_Area2D_body_entered(body):
    if "Player" in body.name:
        body.dead()

For Player:

func dead():
    hp -= 1
    if hp <= 0:
        is_dead = true
        velocity = Vector2(0,0)
        $AnimatedSprite/AnimationPlayer.play("dead")
        $CollisionShape2D.set_deferred("disabled",true)
    else:
        is_hurt = true
        velocity.x = 0
        $AnimatedSprite/AnimationPlayer.play("hurt")

Is there a possible way to do so?

Thanks in advance!

Comments

  • MegalomaniakMegalomaniak Posts: 2,027Admin
    edited March 25

    Maybe try using the area nodes entered and exited signals?

  • dealchilldealchill Posts: 6Member

    @Megalomaniak said:
    Maybe try using the area nodes entered and exited signals?

    Yes, I already did that. But can the entered signal be triggered constantly without another body exit it?

  • vmjcvvmjcv Posts: 17Member

    i don't think reduced HP is related to frame rate is a good idea even if you can trigger the signal continuously.
    maybe you can open a timer in _on_Area2D_body_entered,and close it in _on_Area2D_body_exited.
    If you do this, you can know the actual damage value, regardless of the quality of the machine.

  • MegalomaniakMegalomaniak Posts: 2,027Admin

    @dealchill said:
    Yes, I already did that. But can the entered signal be triggered constantly without another body exit it?

    Well, I mean you are setting the state of the boolean variables, which will remain true. You'll want to make sure that they are set to false in the right circumstances. body exited signal might be a way to achieve that. But maybe you are already dealing with that in some other way.

  • dealchilldealchill Posts: 6Member

    Thanks for the replies guys! @Megalomaniak @vmjcv

    I just settled for an Animation Player that loops the disabled value for the Collision2D.

  • vmjcvvmjcv Posts: 17Member

    I don't quite understand your solution, but I'm glad you solved the problem

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file