So, I have a simple 3D game with a player, an enemy, and walls. I've figured out how to detect collisions between my player (RigidBody) and an enemy or wall (Static Body). I want to know how can I group all the enemies so that the player collision can detect whether it's collided with an enemy or a wall, kind of like tags in Unity?
2 Answers
Godot has groups, that work like Unity tags but are more powerful and versatile.
You can assign nodes to one or more groups, and check for group membership when a collision occurs.
Objects in the same group can belong to different layers. If you don't need layer-based collisions, just use groups; otherwise, Arian Keshvari's answer is the additional way to go.
-
\$\begingroup\$ Yes, what I was looking for. \$\endgroup\$ Commented Mar 25, 2022 at 19:20
kind of like tags in unity
that's what you should do in Godot too, but not the tag, rather the layer.
For instance, you can put all of your enemies in a layer named Enemy
or the player in a layer named Player
. It's called Collision Layer
on Godot and the Collision Mask
is just like the Layer Collision Matrix
in Unity and can define which layers can collide with each other.
If you need to know the layer of an object you just have to use this code:
object.get_collision_layer()
And it can be used to determine further actions like taking damage, interacting with NPCs, opening doors, etc.
-
\$\begingroup\$ ok I will keep this in mind \$\endgroup\$ Commented Mar 25, 2022 at 19:19