I was exploring the topic of a balloon tower defence game idea. And I’m stuck on how I would code the portion of Attacking the first target. I had an idea to just put a value inside the balloon every time they come out of spawn and that increases as the round goes on. but the problem with that is if another balloon gets ahead of another the attacking would get all messed up. Any Solutions to fix this? or a better way to code this portion.
You could use raycasting and detect if the ray doesn’t hit another balloon, then that means it’s the first and there are no balloons ahead of it.
Another way is to just calculate the magnitude from the balloon to the exit door. Get the one with the lowest magnitude and detect if it is within range of the tower and if it is then initiate the attacking process.
However, I much prefer the value method and it seems a lot better.
Have value x be how far down the track they have moved. Every time you update their position, add to x. Then you can have a consistent way of tracking which balloon has moved the farthest. (loop through balloons in range and find one with greatest x value)
put all the enemies in an array in a script. using table.insert will insert an object into the #table+1. from there you can use magnitudes and check if the enemy is within a certain magnitude. (magnitudes are distanced over often vector3 usage). if it is in it make it attack the first one, if the first one isn’t in it check for the other ones in the table making it only attack the first one in range.
I could use ray casting the only problem what if there is a turn in the map and with magnitude the problem is if i have a loop in the map and a balloon get closer to spawn then another balloon with be father from spawn
Yeah I noticed that. What I would just do what other people recommended and keep a table of all the objects and have a value inside of each object (balloon). Every time the position is updated just add 1 to that value and then get the one with the highest value to be attacked (if they are in range of course)
Thank you so much thinking about that is making think it will work
hey I was in coding this and is there a simple way to fire a function when the position is updated