I’m up to the rebalancing lecture in the course, and one thing I really want to achieve before going forward involves changing how the target locator works. Currently, the towers will constantly seek for the closest target, switching targets other targets get closer, even if their initial target is still within their firing range. My goal is to make each tower stick to one target until it goes out of range. I was trying to see if I understood enough about coding in C# to change this myself, but I can’t get it to work and was hoping for some pointers.
I added a new bool called “targetAcquired” which is initialised as “false” and added code to the bottom of the FindClosestTarget method:
I then changed Update so that FindClosestTarget is only performed if the target has not been acquired.
When I run the game, the tower targets the first enemy correctly, and doesn’t select a new target even when others are closer. However, it does not select a new target even when the initial target is out of range. It just looks at it as it moves around the map, and if it is destroyed and respawned, it follows it from the start again.
Can someone tell me where I’m going wrong?