using UnityEngine;

public class BitBlast : MonoBehaviour
{
	public Transform prefab;

	public float Speed = 7f;
	public int Count = 4;

	private bool done = false;

    void Update()
    {
        if(!done)
		{
			done = true;
			for (int i = 0; i<Count; i++)
			{
				Transform bit = Instantiate(prefab);
				bit.position = transform.position;

				float sign = Mathf.Sign(1 - i);
				Vector2 rand = Random.insideUnitCircle;
				Vector2 dir = new Vector2(sign * Mathf.Abs(rand.x), rand.y);

				Rigidbody2D r2 = bit.GetComponent<Rigidbody2D>();
				if(r2 !=null)
				{
					Vector2 accel = dir * Speed;
					Vector2 force = r2.mass * accel / Time.fixedDeltaTime;
					r2.AddForce(force);
				}
			}

			Destroy(gameObject);
		}
    }
}
