#pragma strict
var leftMost: float;
var rightMost: float;
var topMost: float;
var bottomMost: float;
var targets: Array;
var targets2: Array;
var go: GameObject;
var go2: GameObject;
var go3: GameObject;
var count:int;
var cController: CrosshairController;

function Start () {
	cController = GameObject.FindGameObjectWithTag("Crosshair").GetComponent(CrosshairController);
	
}

function Update () {
	transform.Translate(Vector3.forward * 10 * Time.deltaTime);
	
	targets = GameObject.FindGameObjectsWithTag("Target");
	targets2 = new Array();
	if(targets.length >= 3){
		targets2.Push(targets[0]);
		targets2.Push(targets[1]);
		targets2.Push(targets[2]);
		go = targets2[0];
		this.gameObject.transform.position = Vector3.MoveTowards(this.gameObject.transform.position,go.transform.position,Time.deltaTime * 20);
		
		go2 = targets2[1];
		this.gameObject.transform.position = Vector3.MoveTowards(this.gameObject.transform.position,go2.transform.position,Time.deltaTime * 20);
		
		go3 = targets2[2];
		this.gameObject.transform.position = Vector3.MoveTowards(this.gameObject.transform.position,go3.transform.position,Time.deltaTime * 20);
		
	}else if(targets.length == 2){
		targets2.Push(targets[0]);
		targets2.Push(targets[1]);
		
		go = targets2[0];
		this.gameObject.transform.position = Vector3.MoveTowards(this.gameObject.transform.position,go.transform.position,Time.deltaTime * 20);
		
		go2 = targets2[1];
		this.gameObject.transform.position = Vector3.MoveTowards(this.gameObject.transform.position,go2.transform.position,Time.deltaTime * 20);
		
	}else if(targets.length == 1){
		targets2.Push(targets[0]);
		
		go = targets2[0];
		this.gameObject.transform.position = Vector3.MoveTowards(this.gameObject.transform.position,go.transform.position,Time.deltaTime * 20);
	}
	/*
	for(var i = 0; i < targets2.length; i++){
		go = targets2[i];
		this.gameObject.transform.position = Vector3.MoveTowards(this.gameObject.transform.position,go.transform.position,Time.deltaTime * 50);
	}
	*/
	leftMost = Camera.main.ScreenToWorldPoint(Vector3(0,0,0)).x;
	rightMost = Camera.main.ScreenToWorldPoint(Vector3(Screen.width,0,0)).x;
	
	bottomMost = Camera.main.ScreenToWorldPoint(Vector3(0,0,0)).y;
	topMost = Camera.main.ScreenToWorldPoint(Vector3(0,Screen.height,0)).y;
	
	
	if(transform.position.x < leftMost){
		Destroy(this.gameObject);
	}
	
	if(transform.position.x > rightMost){
		Destroy(this.gameObject);
	}
	
	if(transform.position.y < bottomMost){
		Destroy(this.gameObject);
	}
	
	if(transform.position.y  > topMost){
		Destroy(this.gameObject);
	}

}

function OnCollisionEnter2D(otherObject: Collision2D){
	if(otherObject.gameObject.tag == "Target"){
		Destroy(this.gameObject);
		Destroy(otherObject.gameObject);
		cController.DecreaseTarget(1);
	}
	
}