I am trying to make a game like bloon tower defence 5 and i have no idea how I could make the tower or npc that you place down target the npc that is the closest to the end but still in range. I have looked everywhere to try and find something that could help me create this script and I've found nothing so I have no idea where to start. Could someone give me an example or point me in the right direction, Thanks in advance.
How would I make npc shoot other npcs that are the closest to the end but still in range?
1 answer
Hello! I am going to try to answer your question to the best of my abilities and hopefully lead you in the right direction. I would suggest using something called Magnitude. Magnitude lets you find the difference between positions. Here's an example of Magnitude in use:
Bloon = Vector3.new(0,0,0)
End = Vector3.new(0,0,-20)
print((Bloon-End).magnitude)
This script will print 20 because the difference between the two values is 20. Now if you wanted to use multiple bloons and find the distance between the closest bloon and the end of your level you could do this:
Bloon1 = Vector3.new(0,0,0)
Bloon2 = Vector3.new(0,0,2)
Bloon3 = Vector3.new(0,0,-5)
End = Vector3.new(0,0,-20)
positions = {}
table.insert(positions,(Bloon1-End).magnitude)
table.insert(positions,(Bloon2-End).magnitude)
table.insert(positions,(Bloon3-End).magnitude)
table.sort(positions)
print(positions[1])
With the code above you could easily add if statements to let your script know which bloon is the closest. Here is an example of finding the bloon who's closest to the end:
if positions[1] == (Bloon1-End).magnitude then
print("Bloon1 is the closest")
end
if positions[1] == (Bloon2-End).magnitude then
print("Bloon2 is the closest")
end
if positions[1] == (Bloon3-End).magnitude then
print("Bloon3 is the closest")
end
The script above will work with the multi-bloon script. Magnitude can also be used to find if something is in range.
Hopefully that helped!
This discussion is preserved as it was. New answers, comments, and votes are closed.
