using UnityEngine;
using System.Collections;

public class FireProjectile3 : MonoBehaviour
{
	public GameObject projectile;

	public Controller player;

	public Transform launchPoint;

	private float shotCounter;

	public float fireDelay;

	// Use this for initialization
	void Start ()
	{
		if (FindObjectOfType<Controller> () != null)
		{
			player = FindObjectOfType<Controller> ();

			shotCounter = 2f;
		}
	}

	// Update is called once per frame
	void Update()
	{
		if (FindObjectOfType<Controller> () != null) 
		{
			shotCounter -= Time.deltaTime;

			if (player.transform.position.x > transform.position.x && shotCounter < 0) 
			{
				Instantiate (projectile, launchPoint.position, launchPoint.rotation);
				shotCounter = fireDelay;
			}

			if (player.transform.position.x < transform.position.x && shotCounter < 0) 
			{
				Instantiate (projectile, launchPoint.position, launchPoint.rotation);
				shotCounter = fireDelay;
			}
		}
	}
}
