Advertisement
Placido_GDD

Basic Gun Coroutines

Jun 30th, 2021 (edited)
647
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class SoldierRifle : MonoBehaviour
  7. {
  8.     public bool gunCombat;
  9.     protected bool is_Shooting;
  10.     public int max_bcount;
  11.     public float reloadDur;
  12.     public float shottimeLag;
  13.     protected int b_count;
  14.     void Start()
  15.     {
  16.         b_count = 0;
  17.         InvokeRepeating("AI_Routine",0.0f,1.0f);
  18.     }
  19.    
  20.     bool isShooting()
  21.     {
  22.         if(b_count < max_bcount)
  23.         {
  24.             is_Shooting = true;
  25.             return true;
  26.         }
  27.         else
  28.         {
  29.             is_Shooting = false;
  30.             return false;
  31.         }
  32.     }
  33.    
  34.    
  35.     public void AI_Routine()
  36.     {
  37.         if(gunCombat == true)
  38.         {
  39.             ShootGun();
  40.         }
  41.     }
  42.    
  43.     public void ShootGun()
  44.     {
  45.             StartCoroutine(InstantiateBulletCoroutine());
  46.             Reloading();
  47.     }
  48.  
  49.    
  50.    
  51.     void Reloading()
  52.     {
  53.         if((gunCombat == true)&&(is_Shooting == false))
  54.         {
  55.             StartCoroutine(ReloadingCoroutine());
  56.         }
  57.        
  58.     }
  59.    
  60.     IEnumerator InstantiateBulletCoroutine()
  61.     {
  62.         while(b_count < max_bcount)
  63.         {
  64.             yield return new  WaitForSecondsRealtime(shottimeLag);
  65.             //do instantiate bullet things//           
  66.             //                            //
  67.             Debug.Log("Spawn Bullet");
  68.             b_count++;
  69.         }
  70.     }
  71.     IEnumerator ReloadingCoroutine()
  72.     {
  73.         gunCombat = false;
  74.         yield return new WaitWhile(isShooting);
  75.         Debug.Log("Reloading");
  76.         yield return new  WaitForSecondsRealtime(reloadDur);
  77.         b_count = 0;
  78.         Debug.Log("Done Reloading");
  79.         StopAllCoroutines();
  80.     }
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement