0 votes

Hello, I am fairly new to Godot and game development in general, and i'm making a top-down shooter as my first game in the engine. For my first enemy, i am having a KinematicBody2D to chase the player(who is also a KinematicBody2D) and deal damage when colliding with it. This is the code for the enemy's AI:

extends "res://AI.gd"

func run():
    if initialized:
        var direction = body.global_position.direction_to(player.global_position)
        direction *= actor_values.speed

        for i in body.get_slide_count():
            var collision = body.get_slide_collision(i)
            if collision.collider.is_in_group("AnimeHater"):
                if collision.collider.has_method("handle_hit"):

and this is how the player is programmed to take damage:

func handle_hit(strength):
if invul_timer.is_stopped():
    health.current_health -= strength 
    health.current_health = clamp(health.current_health, 0, 100)
    health_label.text = ("Health: " +str(health.current_health))
    if health.current_health == 0:

The damage on the player does occur when he is first touched, but if the player stays still, sometimes he takes damage as soon as the invulnerability is over (which is the intended behaviour), and sometimes the next time he takes damage is seemingly random, as shown in this video:

What can i do to have the player character always being damaged as soon as his invulnerability period is over?

in Engine by (22 points)

1 Answer

0 votes

The problem here is you are only detecting collisions from moveandslide() results, which is correct if you want to detect walls or the ground but it forces you to move every time to keep detecting collisions. In this case, where you and the enemy could stay still without moving, I would recommend you to use Area2D, where you can connect it with two signals: bodyentered and boyexited. Listening them you can detect if the enemy is overlapping your player when the invulnerability timers finished and damage it again.

by (70 points)
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 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 webmaster@godotengine.org with your username.