I just added new valiable to know is player already firing or not
With this code you can shoot by multiple input and switch between mouse and keyboard without any bugs.
private void Fire()
{
if (Input.GetButtonDown("Fire1") && !firing)
{
firingCoroutine = StartCoroutine(FireContinuously());
}
if (Input.GetButtonUp("Fire1") && !Input.GetButton("Fire1"))
{
StopCoroutine(firingCoroutine);
firing = false;
}
}
IEnumerator FireContinuously()
{
while (true)
{
firing = true;
GameObject laser = Instantiate(laserPrefab, transform.position, Quaternion.identity);
laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
yield return new WaitForSeconds(projectileFiringPeriod);
}
}