using UnityEngine;
using System.Collections;

public class Projectile52 : MonoBehaviour
{
	public Controller2 player;

	public GameObject impactEffect;

	public float speed;

	private Transform target;

	public float damageToGive;

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

			target = player.transform;
		}
	}

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

	void OnTriggerEnter2D(Collider2D other)
	{
		if (other.name != "Enemy")
		{
			Instantiate(impactEffect, transform.position, transform.rotation);
			Destroy (gameObject);

			if (other.name == "Player 1")
			{
				other.GetComponent<PlayerHealth> ().giveDamage (damageToGive);
			}

			if (other.name == "Player 2")
			{
				other.GetComponent<PlayerHealth> ().giveDamage (damageToGive);
			}
		}
	}
}
