Godot Version
v4.2
Question
Hi all. I have followd through the “Squash the Creeps” first 3D game tutorial and all works as expected. I figured I would ry to expand upon the game by adding a laser beam to the front of the player character and have this “squash the creeps” in addition to my jumping on them. I was able to make a beam that I toggle on and off with a trigger pull, and I used the below script in my Area3D node (child of player) to run the squash function when the beam body was entered:
func _on_body_entered(body):
get_tree().call_group("mob", "squash")
The issue with this is that call group calls the function on all on-screen mobs, meaning that they all die instead of just the one hit. I understand that in the original code the jumping on the head of a mob killed only the one mob because of the collision.get_collider() identifying the single mob that was involved in the player collision. How do I either call squash on only the mob that the beam hit (and identify which one was hit) using a direct call or emit?
Below is the code showing the instantiation of the mobs:
func _on_mob_timer_timeout():
# create a new instance of the mob scene
var mob = mob_scene.instantiate()
# Choose a random location on the SpawnPath
# We store a reference to the SpawnLocaiton node
var mob_spawn_location = get_node("SpawnPath/SpawnLocation")
# And give it a random offset
mob_spawn_location.progress_ratio = randf()
var player_position = $Player.position
mob.initialize(mob_spawn_location.position, player_position)
# Spawn the mob by adding it to the main scene
add_child(mob)
# We connect the mob to the score lable to update the code upon squashing one.
mob.squashed.connect($UserInterface/ScoreLabel._on_mob_squashed.bind())