Bomb_Fail

 avatar
unknown
csharp
4 years ago
2.5 kB
6
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bomb : MonoBehaviour
{
    public LayerMask blockLayer;
    public GameObject explodeSide;

    private Powers powers;
    private Animator animator;

    private void Start()
    {
       powers = GameObject.FindObjectOfType<Powers>();
        animator = GetComponent<Animator>();
        StartCoroutine(fusing());
    }

    IEnumerator fusing()
    {
        float delay = powers.fuseTime;

        while(delay > 0)
        {
            delay -= Time.deltaTime;
            yield return null;
        }

        animator.SetTrigger("explode");
        Explode();
    }

    void Explode()
    {
        //Direction for ray
        Vector2 rayDirection = Vector2.right;

        //Raycast to all 4 directions
        for (int i = 0; i < 4; i++)
        {
            //Create child gameObject explode
            GameObject childExplodeDown = Instantiate(explodeSide, new Vector2(transform.position.x, transform.position.y), explodeSide.transform.localRotation);
            childExplodeDown.transform.parent = transform;
            childExplodeDown.transform.eulerAngles = Vector3.forward * i * 90;

            Quaternion rotDegree = Quaternion.Euler(0, 0, 90 * i);
            rayDirection = rotDegree * rayDirection;

            //Cast with explodeSize
            for(int o = 0; o < powers.explodeSize; o++)
            {
                RaycastHit2D hit = Physics2D.Raycast(new Vector2(transform.position.x + o, transform.position.y), rayDirection, 0.65f, blockLayer);
               
                if(hit.collider == null)
                {
                    //new Vector2(transform.position.x + 1 + o * 0.5f, transform.position.y);
                    childExplodeDown.transform.position = new Vector2(transform.position.x, transform.position.y) + rayDirection * (1 + o * 0.5f);
                    childExplodeDown.transform.localScale = new Vector2(transform.localScale.x + o, transform.localScale.y);
                }

                else
                {
                    //If child explode is having same position with parent then it gets destroyed.
                    if (childExplodeDown.transform.position == transform.position)
                    {
                        Destroy(childExplodeDown);
                    }

                    //return;
                }
            }

        }
    }
Editor is loading...