#pragma strict
var leftMost: float;
var rightMost: float;
var topMost: float;
var bottomMost: float;
var speed: float;
var materials:Material[];
var width: float;
var percentage: float;
var i:int;
var positionX:int;
var count:int;


function Start () {
	if(Application.loadedLevel == 2){
		width = Camera.main.ScreenToWorldPoint(Vector3(Screen.width,0,0)).x;//Width of the screen
		width = width - (width*0.2);
		positionX = transform.position.x + (width * 0.2);
		percentage = 0.2;
		i=0;
	}
	
	InvokeRepeating("changeMaterial",0.5,0.5);
	
}

function Update () {
	speed = Random.Range(5,7);
	transform.Translate(Vector3.right * speed * Time.deltaTime);
	
	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){
		transform.position.x = rightMost;
	}
	
	if(transform.position.x > rightMost){
		transform.position.x = leftMost;
		count = 2;
		positionX = transform.position.x + (Camera.main.ScreenToWorldPoint(Vector3(Screen.width,0,0)).x * 0.2);
	}
	
	if(transform.position.y < bottomMost){
		transform.position.y = topMost;
	}
	
	if(transform.position.y  > topMost){
		transform.position.y = bottomMost;
	}
}

function changeMaterial(){
	if(Application.loadedLevel == 2){
		if(transform.position.x > positionX && i != 5)
		{		
			this.renderer.material = materials[i];		
			positionX = transform.position.x + count;
			count +=2;
			i++;
		}
		
	}
}

function OnCollisionEnter2D(otherObject: Collision2D){
	var count:int = 0;
	var hit : ContactPoint;
	
	if(otherObject.gameObject.tag == "AmmoBox"){
		this.gameObject.transform.rotation.z = 0;
		
	}else if(otherObject.gameObject.tag == "Target"){
		this.gameObject.transform.rotation.z = 0;
		otherObject.gameObject.rigidbody2D.AddForce(Vector3.right * 50);
	}
	
}









