Skip to content

Instantly share code, notes, and snippets.

@bram-dingelstad
Created August 30, 2022 21:31
Show Gist options
  • Save bram-dingelstad/8faf9feac562f28dfbb77ccf1158605c to your computer and use it in GitHub Desktop.
Save bram-dingelstad/8faf9feac562f28dfbb77ccf1158605c to your computer and use it in GitHub Desktop.
Mesh with automatic hitboxes script for Godot
tool
extends EditorScenePostImport
var main_scene
func post_import(scene):
main_scene = scene
add_hitboxes_to_structures(scene)
print('Imported!')
return scene
func add_hitboxes_to_structures(scene):
# NOTE: If name exactly matches this, ignore and don't make hitbox
# NOTE: Can be extended to include RegEx for more detailed matching
var disallow_list = [
'Stairs',
'NeighbourhoodDog'
]
for child in scene.get_children():
var matches = false
# NOTE: Place to change matching algorithm
for entry in disallow_list:
if entry in child.name:
matches = true
break
if not matches and child is MeshInstance:
# NOTE: This example uses the create_trimesh_collision, can be done with other methods as well.
# NOTE: e.g https://docs.godotengine.org/en/stable/classes/class_meshinstance.html?highlight=create_#method-descriptions
child.create_trimesh_collision()
var static_body = child.get_node('%s_col' % child.name)
# NOTE: Rename auto generated trimesh to StaticBody
static_body.name = 'StaticBody'
static_body.owner = main_scene
for node in static_body.get_children():
node.owner = main_scene
# NOTE: Keep going into children
if not matches:
add_hitboxes_to_structures(child)
else:
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment