Search code examples
game-developmentgodotgdscript

Godot how to get collision layer of colliding objects programmatically?


I have 3 collision layers:

  1. Player
  2. Coin
  3. Enemy

My nodes are colliding well, but when my Player collides with the Coin or the Enemy i can't differentiate between the two of them. Normally i would do this by setting collision groups but if the layers are already set it may be easier to check the layer.

Posible solution using groups

func _on_Player_area_entered(area):
  if area.is_in_group('enemies'):
    pass
  if area.is_in_group('coins'):
    pass

How can i get the Coin and the Enemy collision layers programmatically when they collide with Player?


Solution

  • I think the best way is to just do what you suggested: Put the coins and enemies in their own Node groups then do is_in_group checks in your collision code.

    If you want to avoid Node groups, another way of achieving this is creating a separate Area2D node in your Player scene that will only collide with coins while the other Area2D collides with enemies.