using UnityEngine;
using System.Collections;

public class Projectile3 : MonoBehaviour
{
	public EnemyMovement enemy;

	public GameObject impactEffect;

	public float speed;

	private Transform target;

	public float damageToGive;

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

			target = enemy.transform;
		}
	}

	// Update is called once per frame
	void Update ()
	{
		if (FindObjectOfType<EnemyMovement> () != null)
		{
			if (Time.timeScale != 0)
			{
				transform.position = Vector3.MoveTowards (transform.position, target.position, speed);
			}
		}
	}

	void OnTriggerEnter2D(Collider2D other)
	{
		if (other.name == "Enemy" || other.name == "Lightning")
		{
			if (other.name == "Enemy")
			{
				other.GetComponent<EnemyHealth> ().giveDamage (damageToGive);
			}
			Instantiate(impactEffect, transform.position, transform.rotation);
			Destroy (gameObject);
		}
	}
}
