+6 votes

Hey, guys, I really don't get how to use a body_enter function, please explain.
I got two nodes Player and Enemy and they both are KinematicBodies2D.
Enemy node has got an Area2D node in it with script like this:

extends Area2D

func _ready():
    connect("body_enter", self, "_on_enemy_body_enter")


func _on_enemy_body_enter(body):
    print("Collision")
    if (body.get_name == "Player")
        print("Ouch!")

I also have tried to add an Area2D to Player node and to change areas to RigidBodies2D but nothing seems to be working at all.
What am I doing wrong?

asked Jul 7, 2016 in Engine by ezangillz (24 points)

Is there any error in the console or debugger?

This line is missing stuff: if (body.get_name == "Player"). It should be if (body.get_name() == "Player"): (notice the colon at the end).

Also, have you defined shapes for the bodies and areas?

omg, I'm so dumb, thank you a lot! It works perfectly now, but one more thing, please, is it okay for performance and stuff to have such thing? I mean two CollisionShapes and KinematicBody, and if it's not, then is there any way to improve that?enter image description here

The more physics stuff the more the CPU needs to work. But why do you have your enemy as an Area and a Body as child? Isn't better to make the enemy as body (so you don't need to update the parent transform via code)? If you really need the area for some other test, add it as a child of the body.

you're right, it's much better, thank you a lot for your help!

Please log in or register to answer this question.

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.