My solution to fix the bug

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);
	    }
    }
3 Likes

Awesome job figuring out the bug!

Great job taking the initiative to come up with your own solution :slight_smile:

I’ve tried this out and it works! I understand why for the most part but this line confuses me:

if (Input.GetButtonUp("Fire1") && !Input.GetButton("Fire1"))

Privacy & Terms