Exemple #1
0
    public void explode(float force, Vector3 explosionPosition)
    {
        //if fragments were not created before runtime then create them now
        if (fragments.Count == 0 && allowRuntimeFragmentation)
        {
            generateFragments();
        }
        //otherwise unparent and activate them
        else
        {
            foreach (GameObject frag in fragments)
            {
                frag.transform.parent = null;
                frag.SetActive(true);
            }
        }
        //if fragments exist destroy the original
        if (fragments.Count > 0)
        {
            Destroy(gameObject);

            if (force > 0)
            {
                foreach (GameObject gameObject in fragments)
                {
                    Rigidbody2D rigidbody2d  = gameObject.GetComponent <Rigidbody2D>();
                    Vector3     v3Difference = gameObject.transform.position - explosionPosition;
                    Vector2     difference   = new Vector2(v3Difference.x, v3Difference.y);
                    Vector2     newDirection = Vector2Utils.GetSmallVariation(difference.normalized, 5);
                    rigidbody2d.AddForce(newDirection.normalized * force, ForceMode2D.Impulse);
                }
            }
        }
    }