Kinematikbody2d vs area2d

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By calvar

Hi,

I am just learning godot, and I am toying with characters and tilemaps. I have been able to make kinematikbody2d characters colide with the tiles without problem, but I read that kinematicbody is used for player characters and that for enemies it is better to use area2d. I find area2d more difficult to use because they don’t have move_and_collide method, and collisions have been a mess. When would one preffer area2d over kinematicbody2d for characters, even if they are not player controlled?

Thank you.

:bust_in_silhouette: Reply From: kidscancode

I wouldn’t say Area2D is better for enemies, but it is better in some situations. You really need to understand how each node works so you can choose what will work best in your particular game.

Area2D is much simpler than KinematicBody2D. It doesn’t have collision, it just detects overlap. Moving it is a matter of updating its position:

extends Area2D

var velocity = Vector2(100, 0)

func _physics_process(delta):
    position += velocity * delta

To detect when an area overlaps another object you need to use its signals: either body_entered (for PhysicsObject2D nodes) or area_entered (for other areas). Connect the signal and you can do something like

func _on_Area2D_body_entered(body):
    if body.name == "Player":
        body.take_damage(1)
        queue_free()

In this example, the enemy would damage the player on contact and then disappear.

For more information and examples, see Physics Introduction in the official docs.

Ok, thank you. I though for a moment that kinematicbody was meant to be used for player controled characters and did not understood why. I’ll read more carefully.

calvar | 2019-06-23 01:58