- Home /
multiple targeting
how to loop the "var target : Transform;" my aim is to set a range for the target that when they reach the range, that object will become the target
Answer by hoy_smallfry · Feb 25, 2013 at 01:54 AM
1) set all your targets to the same tag
2) get an array of all the targets with GameObject.FindObjectsWithTag()
3) create an empty list of game objects; this one will store the targets you can shoot
4) loop through all the objects in the array (See Example 2)
5) for each target, find the vector between the game object and your character using vector substraction
6) with this vector, find the square magnitude.
7) use an if-statement to check if the square magnitude is less than range suared.
8) If it is, that means that your target is within range, so you should add it to the list of targets you can shoot.
9) when the loop is done, you should have all the targets that can be shot at within range.
What you need is to try. If you had a little you would have found out that the link in step 2 of jbarba's answer is your answer.
Your answer
Follow this Question
Related Questions
Compilor Error using Dictonary collection 1 Answer
Targeting nearest enemy ! 1 Answer
Breaking out of a loop 2 Answers
How do I set a target gameobject within another gameobjects component? 1 Answer
General Questions 1 Answer