Advertisement
LeeMace

Continuous Shooting

Nov 3rd, 2023
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.77 KB | Gaming | 0 0
  1.  [SerializeField] GameObject laserFire;
  2.  [SerializeField] float projectileSpeed = 20f;
  3.  private float projectileFiringPeriod = 0.1f;
  4.  
  5.  Coroutine firingCoroutine;
  6.  
  7.  void Update()
  8.  {
  9.      Fire();
  10.  }
  11.  
  12.  IEnumerator FireContinuously() {
  13.  
  14.      while (true)
  15.      {
  16.          GameObject laser = Instantiate(laserFire, transform.position, Quaternion.identity) as GameObject;
  17.          laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
  18.          yield return new WaitForSeconds(projectileFiringPeriod);
  19.      }
  20.  }
  21.  
  22.  private void Fire() {
  23.  
  24.      if (Input.GetButtonDown("Fire1"))
  25.      {
  26.        firingCoroutine =  StartCoroutine(FireContinuously());
  27.      }
  28.      if (Input.GetButtonUp("Fire1"))
  29.      {
  30.          StopCoroutine(firingCoroutine);
  31.      }
  32.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement