Tower Defense - Target Lock [Beginner]
Hey guys. Im a beginner and Im kind of stuck with a problem my brain can't handle.
Im currently working on a tower defense. I spawn minions into an enemy group, for each enemy in the group I check if the target is in tower range. If yes it returns the enemy if not its nulled. (I kind of got the process from another TD I found online)
Thats the chose target function:
func choose_target(): var pos = get_global_position() for enemy in get_tree().get_nodes_in_group("enemy"): if pos.distance_to(enemy.get_global_position()) <= fire_range && enemy.get_health() > 0: target = enemy else: target = null return target
Now to my problem:
Since the tower checkes randomly for each enemy the damage is all over the place, Ofc the most logical thing is attack the closest minion and lock on it till its dead. I made it work with attacking the closest with a local variable that saves the last attack distance and compares it to the new one.
However I can't figure out how to lock on a target. I think my biggest problem is the "for enemy in" loop. And since I kind of tried options to the best of my knowledge for a couple of hours now Im kind of lost how to make this work.