Help collision object

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

Good time of day… No one answered the last request, well, I’m changing my approach …
So there is an object and its collision. Let’s say it will be a typical “zombie”. Gg (The Main character) holds an axe in his hands, he has his own collision. How to make it so that when you touch the “axe” of a “zombie”, he receives damage, and not to one “zombie”, but to all. So to say “prefabricated” (Hello, Unity)? Let’s say there are “zombies”, “zombi_2”, “and so on” on the scene, and in order not to make a separate pile of code for each of them, it was possible to use one collision check and a collision for all, even “duplicated” objects?

:bust_in_silhouette: Reply From: timothybrentwood

What you’re looking for is a hitbox/hurtbox system:

This method is not suitable because if you duplicate these barrels, the collision does not work on them. That is, it is necessary to write the path to the collision again for each almost.

VarionDrakon | 2021-06-25 12:50

It most certainly works for any duplicated scenes - or in your case Zombies.

You need to make your Zombie its own scene which should have a minimum hierarchy like this:

>Node2D (or something that inherits from Node2D)
>>Area2D
>>>CollisionPolygon2D (or CollisionShape2D)

Then select your Area2D, go to the right hand side of the Editor window and press the Node tab then press the Groups tab and add “hurtbox” as a group. Alternatively, add add_to_group("hurtbox") in the _ready() function of the script attached to the Area2D. Likewise with your player’s axe, but add it to the “hitbox” group. Groups — Godot Engine (latest) documentation in English

Make sure your collision is set up properly on both your player’s hitbox and zombie’s hurtbox - if you haven’t played with collision the defaults should work fine. Physics introduction — Godot Engine (stable) documentation in English

Then drag and drop your Zombie.tscn scene from the FileSystem box (bottom left) by default into whatever your world scene is to create instances of zombies. Alternatively load() the Zombie.tscn into a function in your world scene and instance() then add_child() it to create instances of zombies. Creating instances — Godot Engine (stable) documentation in English

Prefabs in Unity = Scenes in Godot. https://docs.godotengine.org/en/stable/getting_started/step_by_step/scenes_and_nodes.html

timothybrentwood | 2021-06-25 14:33

To add to this, if your equivalent of the take_damage() function shown in the video resides in your root node (the Node2D listed in the hierarchy in the comment above) you’ll need to call area.get_parent().take_damage() instead of just area.take_damage() in which case it might be better to rewrite the function to add some crash safety:

func _on_SwordHit_area_entered(area):
    if not area.is_in_group("hurtbox"):
        pass
    elif not is_instance_valid(area.get_parent()):
        pass
    else:
        area.get_parent().take_damage()

timothybrentwood | 2021-06-25 14:48

Of course, thank you for the detailed manual, but the question remains, how to use these groups and collisions from it later, if the addition to the groups comes from different scripts and scenes, and then all the scenes are assembled into one?

VarionDrakon | 2021-06-25 17:19