- Home /
Question by
oinkoinkflapflap · Apr 16, 2014 at 03:21 PM ·
taganglefindunlimited
making Enemy detecting script unlimited
i have a script that i need to detect if there is an object of a certain tag infront of it, it works, so long as i know how many of the objects there will be:
var targets : GameObject[];
function Update () {
var targets = GameObject.FindGameObjectsWithTag("Target");
var forward2 = transform.forward;
var targetDir = (targets[1].transform.position - transform.position);
var angle = Vector3.Angle(targetDir, forward2);
if(angle <= 20){
//do it's thing...
}
var targetDir2 = (targets[2].transform.position - transform.position);
var angle2 = Vector3.Angle(targetDir2, forward2);
if(angle2 <= 20){
//do it's thing...
}
var targetDir3 = (targets[3].transform.position - transform.position);
var angle3 = Vector3.Angle(targetDir3, forward2);
if(angle3 <= 20){
//do it's thing...
}
}
i need to work with any object of this tag within the angle, and there could be unlimited, thanks in advance!
Comment
Your answer
Follow this Question
Related Questions
character collider colliding with another character collider 1 Answer
Whats the best way to access other Game Objects? 2 Answers
Find how many are there in the scene 1 Answer
Find GameObject in Canvas 2 Answers
Camera.main look up 1 Answer