- Home /
Attack multiple targets in the same tag?
I'm Ryan and I'm going to make a TD game with a Energy tower.
The problem is that i would like to make a script to attack enemies and I don't know how to attack multiple enemies... Could somebody help me? And I'm a little bit confused so I've to change a lot... Thanks for looking...
Here is my script:
Can you confirm how you want it to behave? e.g. attack enemies at exactly the same time (fire multiple shots at once), attack one after the other, attack closest enemy?
Answer by Inan-Evin · Jan 17, 2014 at 05:26 PM
Well I can't edit your code directly cause I can have problem with syntaxes. But I can tell you the logic which is better way for you to understand things, always firstly understand the logic. What you need is creating an array, array is a variable of kind which includes multiple variables of definited kinds.
http://docs.unity3d.com/Documentation/ScriptReference/Array.html
You are going to create the array of enemies :
var myTargets : Transform[];
And you can use array codes to add objects to your array, you can check gameobjects tagged as enemy with this :
myTargets = GameObject.FindGameObjectsWithTag("Enemy");
// ( there may be a better usage i just wanna give some examples )
by this, you check the gameobjects tagged as enemy, and make them into your array. ( As I said I'm not sure about that, there may be a better way to add objects to your array )
Next thing you wanna do is make your object select random object from enemy array ( you can adjust the "random" like closest or farest, just seek in here to find how to use arrays, a lot of people asks about them )
Then you are gonna make your object to attack the selected enemy in array, for a amount of time, which can be determined or randomed also, then you are going to make your object to select another object in array. Sorry can't give the exact code, just seek in the forums and here. Good luck !