differentiate collisions with different objects

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

for example you got the player that can do multiple attacks how can i make it so that different attanks damage enemies differently without damaging every enemy

:bust_in_silhouette: Reply From: BigTuna675

Hi there, can you clarify your question a bit? Do you mean that the player has multiple attack styles, but you only want certain enemies to be hit by certain attacks?

If so, you could use Area2D for the collision of the attacks, and then change the physics layer of the Area2D to represent the corresponding attack style.

For example, Attack #1 and Enemy #1 could be on physics layer 1, but Attack #2 and Enemy #2 can be on physics layer 2. If you set up the layers and masks properly, then Enemy #1 can’t be hit by Attack #2, and Enemy #2 can’t be hit by attack #1. Is something like that what you’re looking to achieve?

Or perhaps more simply, you could use a tag system. Give Enemy #1 and Enemy #2 different tags (one object can have multiple tags. So maybe you can do something like (Enemy, Goblin) for Enemy #1, and (Enemy, Ghost) for Enemy #2). Then when Attack #1 happens, check that the colliding enemy has the proper tag to be hit by Attack #1.

what i mean is if you want to make the attacks do different damage on the same enemy
though i assume tag stuff can do that? will try.

nukzhy | 2022-06-09 01:04

I see, if that’s the case then I suggest you put the following function on your enemies, and call it on them when an attack happens.

func takeDamage(damage):
    health -= damage
    if health <= 0:
        queue_free()

The “damage” value can be either the damage for attack #1 or attack #2. Does that help at all?

You can call it on them in the collision check on your player’s attack. Your code might look something like this

#This could be the code on your player for attack #1
func _on_attack1_area_entered(area):
    if area.is_in_group("Enemy"):
        if area.has_method("takeDamage"):
            area.takeDamage(attack1Damage)

#This could be the code on your player for attack #2
func _on_attack2_area_entered(area):
    if area.is_in_group("Enemy"):
        if area.has_method("takeDamage"):
            area.takeDamage(attack2Damage)

BigTuna675 | 2022-06-09 13:47