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():

func _process(delta):
    if(state == 2):
        var slime = slimezinho.instance()
    if(state == 3):
        var slime = mini_slimezinhoinho.instance()

#change state
func _on_hit_area_body_entered(enemy):
    state += 1
in Engine by (20 points)
edited by

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
by (3,227 points)
selected 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 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.