0 votes

I put an Area2D into the player called "hit_area", inside it a CollisionShape2D, then plugged a function into the player script so that when the enemy touches the player it would appear another KinematicBody2D in this scene that follows the player, but as soon as I start the game the 2 KinematicBody2D appears without even touching the enemy. How can I fix it?

Here's the code:

extends KinematicBody2D

var state = 1
const slimezinho = preload("res://scenes/slimes/slimezinho.tscn")
const mini_slimezinhoinho = preload("res://scenes/mini_slimezinhoinho.tscn")

func _ready():
    pass

func _process(delta):
    #multiply
    if(state == 2):
        var slime = slimezinho.instance()
        get_parent().add_child(slime)
        slime.set_position(get_node("spawn").get_global_position())
        pass
    if(state == 3):
        var slime = mini_slimezinhoinho.instance()
        get_parent().add_child(slime)
        slime.set_position(get_node("spawn").get_global_position())
        pass
    pass

#change state
func _on_hit_area_body_entered(enemy):
    state += 1
    pass
asked Dec 30, 2019 in Engine by Bárbara Gabriela (20 points)
edited Dec 30, 2019 by Bárbara Gabriela

1 Answer

+1 vote
Best answer

The body_entered signal has the body parameter to reference the intersecting body. Either use a group or the node name to check when body is the enemy.

func _on_hit_area_body_entered(body):
    if body.is_in_group("Enemies"):
        state += 1
answered Dec 30, 2019 by Magso (2,851 points)
selected Dec 30, 2019 by Bárbara Gabriela
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.