using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Shot : MonoBehaviour {

	public int hitLayer;
	public GameObject EplosionParticle;
	// Use this for initialization
	void OnTriggerEnter(Collider col) {
		if(col.gameObject.layer == hitLayer) {
			col.GetComponent<ShipController>().TakeDamage(1); 
			Explode(transform.position);
		}
	}

	public void Explode(Vector3 pos) {
		GetComponentInChildren<Renderer>().enabled = false;
		GameObject expl = Instantiate(EplosionParticle,pos,EplosionParticle.transform.rotation) as GameObject;
		ParticleSystem[] PS = GetComponentsInChildren<ParticleSystem>();
		float max = 0f;
		foreach(ParticleSystem p in PS) {
			if(p.duration > max) {
				max = p.duration;
			}
		}
		Destroy(expl,max+0.5F);
		Destroy(this.gameObject,max+0.5F);
	}
}
