0 votes

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?

Godot version Latest
in Engine by (148 points)
edited by

1 Answer

–1 vote

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

by (3,425 points)

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.

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)
>>>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. https://docs.godotengine.org/en/latest/tutorials/scripting/groups.html

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. https://docs.godotengine.org/en/stable/tutorials/physics/physics_introduction.html

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. https://docs.godotengine.org/en/stable/getting_started/step_by_step/instancing.html

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

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"):
    elif not is_instance_valid(area.get_parent()):

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?

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.