# LookAt probability problem

I have an arena in which there are 8 players. 1 is manual control, others are cpu controled. i made a code in a different function other than the update one, in which i declared a variable to produce random number. Then i used an ‘if’ statement to tell the code that if

``````(randomNumber == 1){
transform.LookAt(enemy1);
}
``````

And there are 8 codes like this for different enemies. I then put the function in the update…but what happens is that the enemies continuosly keep rotating on their axis!! They rotate, like 360 degrees and are looking everywhere!!! is there anyway i can set a time limit in which one enemy targets another for like…10 seconds and then after that, again a random number is generated and again the same enemy looks at another? pls help me out someone pls pls pls

P.S. Okay i solved the problem, but partially. Its not spinning around any more, but once a number is genertaed, it doesnt regenerate even after 10 seconds! so the enemy targets one of the other guys only…but i want it to target all the guys in a certain interval of time(10 seconds)…here is the code:

``````var random : boolean = true;

function RandomNumber() {

if(random){
randomNumber = Random.Range(1 ,7); //GENERATES A RANDOM NUMBER
Debug.Log(randomNumber);  //just for testing
//THIS IS TO INSTRUCT THE CODE TO WAIT BEFORE GENERATING ANOTHER NUMBER
yield WaitForSeconds(0.01);
random = false;
}
yield WaitForSeconds(10);
random = true;

}

function update() {

if(random){
if(randomNumber == 1 && randomNumber!=0){
transform.LookAt(enemy1);

}
if(randomNumber == 2 && randomNumber!=0){
transform.LookAt(enemy2);

}
if(randomNumber == 3 && randomNumber!=0){
transform.LookAt(enemy3);

}
if(randomNumber == 4 && randomNumber!=0){
transform.LookAt(enemy4);

}
if(randomNumber == 5 && randomNumber!=0){
transform.LookAt(enemy5);

}
if(randomNumber == 6 && randomNumber!=0){
transform.LookAt(enemy6);
random = false;

}
if(randomNumber == 7 && randomNumber!=0){
transform.LookAt(enemy7);

}
}

RandomNumber();

}
}
``````

Place all your guy in an array:

``````public Transform[] allTr;
// The target to look at
Transform target;

void Start(){
// Get a target
GetTarget();
// Start a process of repetition of the method
// First call after 1s, each next every 1s
InvokeRepeating("GetTarget",1.0f,1.0f);
}

void Update(){
transform.LookAt(target.position);
}

void GetTarget(){
// Make a list of the Transform
List<Transform>list = new List<Transform>(allTr);
// remove itself
list.Remove(this.transform);
// Get your random
int rand = Random.Range(0, list.Count);
// Get the Transform
target = list[rand];
}
``````

In JS:

``````public var allTr : Transform[];
// The target to look at
private var target : Transform;

function Start(){
// Get a target
GetTarget();
// Start a process of repetition of the method
// First call after 1s, each next every 1s
InvokeRepeating("GetTarget",1.0f,1.0f);
}

function Update(){
transform.LookAt(target.position);
}

function GetTarget(){
// Make a list of the Transform
var list = new List.<Transform>(allTr);
// remove itself
list.Remove(this.transform);
// Get your random
var rand = Random.Range(0, list.Count);
// Get the Transform
target = list[rand];
}
``````