Advertisement
Jon50

Untitled

Jan 27th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.88 KB | None | 0 0
  1.  
  2.  private bool powerUp = false;
  3.  
  4.     void Update()
  5.     {
  6.         Fire();
  7.     }
  8.  
  9.     private void Fire()
  10.     {
  11.         if(powerUp == false)
  12.         {
  13.             if (Input.GetButtonDown("Fire1"))
  14.             {
  15.                 firingCoroutine = StartCoroutine(FireContiuously());
  16.             }
  17.  
  18.             if (Input.GetButtonUp("Fire1"))
  19.             {
  20.                 StopCoroutine(firingCoroutine);
  21.             }
  22.         }
  23.         else
  24.         {
  25.             if (Input.GetButtonDown("Fire1"))
  26.             {
  27.                 firingCoroutine1 = StartCoroutine(FireContiuouslyGun1(10f));
  28.                 firingCoroutine2 = StartCoroutine(FireContiuouslyGun2(10f));
  29.             }
  30.  
  31.             //I don't think this is needed!?
  32.             if (Input.GetButtonUp("Fire1"))
  33.             {
  34.                 StopCoroutine(firingCoroutine1);
  35.                 StopCoroutine(firingCoroutine2);
  36.  
  37.                 //Unless you set the powerUp to false, because you might get infinite powerup, if my logic is correct, which probably isn't.
  38.                 //powerUp = false;
  39.             }
  40.         }
  41.  
  42.         //vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
  43.         //If you want to combine the normal firing with the powerup, use this one
  44.         //vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
  45.        
  46.         // if (Input.GetButtonDown("Fire1"))
  47.         // {
  48.         //     firingCoroutine = StartCoroutine(FireContiuously());
  49.         // }
  50.  
  51.         // if (Input.GetButtonUp("Fire1"))
  52.         // {
  53.         //     StopCoroutine(firingCoroutine);
  54.         // }
  55.  
  56.         // if(powerUp == true)
  57.         // {
  58.         //     if (Input.GetButtonDown("Fire1"))
  59.         //     {
  60.         //         firingCoroutine1 = StartCoroutine(FireContiuouslyGun1(10f));
  61.         //         firingCoroutine2 = StartCoroutine(FireContiuouslyGun2(10f));
  62.         //     }
  63.  
  64.         //     if (Input.GetButtonUp("Fire1"))
  65.         //     {
  66.         //         StopCoroutine(firingCoroutine1);
  67.         //         StopCoroutine(firingCoroutine2);
  68.         //
  69.         //         powerUp = false;
  70.         //     }
  71.         // }
  72.     }
  73.  
  74.     IEnumerator FireContiuously()
  75.     {
  76.         while (true)
  77.         {
  78.             GameObject bullet = Instantiate(projectile, transform.position, Quaternion.identity) as GameObject;
  79.  
  80.             bullet.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
  81.             sfx.GetShootSFX();
  82.  
  83.             yield return new WaitForSeconds(firingPeriod);
  84.         }
  85.  
  86.     }
  87.  
  88.     public void OnTriggerEnter2D(Collider2D doubleBullets)
  89.     {
  90.         if(doubleBullets.gameObject.CompareTag("Double Bullets"))
  91.         {
  92.             Destroy(doubleBullets.gameObject);
  93.             StopCoroutine(FireContiuously() /* <- Isn't this suppose to be firingCoroutine? */ );
  94.  
  95.             powerUp = true;
  96.         }
  97.     }
  98.  
  99.     IEnumerator FireContiuouslyGun1(float timeLimit)
  100.     {
  101.         while (timeLimit > 0f)
  102.         {
  103.             timeLimit -= firingPeriod;
  104.  
  105.             GameObject bullet = Instantiate(projectile, gun1.transform.position, Quaternion.identity) as GameObject;
  106.  
  107.             bullet.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
  108.             sfx.GetShootSFX();
  109.  
  110.             yield return new WaitForSeconds(firingPeriod); //Changed to firingPeriod
  111.         }
  112.  
  113.         powerUp = false;
  114.     }
  115.  
  116.     IEnumerator FireContiuouslyGun2(float timeLimit)
  117.     {
  118.         while (timeLimit > 0f)
  119.         {
  120.             timeLimit -= firingPeriod;
  121.  
  122.             GameObject bullet = Instantiate(projectile, gun2.transform.position, Quaternion.identity) as GameObject;
  123.  
  124.             bullet.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
  125.             sfx.GetShootSFX();
  126.  
  127.             yield return new WaitForSeconds(firingPeriod); //Changed to firingPeriod
  128.         }
  129.  
  130.         powerUp = false;
  131.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement