Player doesn't stop firing

Hello together,

first of all thanks for the great course! :slight_smile:

I only have one issue when i start firing (left Mouse_Button or Space) the player doesn’t stop when i release the button.

I watched the lesson several times and can’t find the problem here is my code:

public class LD_Shooter : MonoBehaviour
{
[Header(“General”)]
[SerializeField] GameObject projectilePrefab;
[SerializeField] float projectileSpeed = 10f;
[SerializeField] float projectileLifetime = 10f;
[SerializeField] float firingRatePlayer = 1f;
//[SerializeField] float baseFiringRate = 0.2f;

[Header("AI")]
[SerializeField] float firingRateAiMin = 1f;
//[SerializeField] float minimumFiringRate = 0.1f; 
[SerializeField] float firingRateAiMax = 0.4f;
[SerializeField] float firingRateAI = 0.2f;
[SerializeField] bool useAI;

public bool isFiring;
LD_AudioPlayer audioPlayer;

Coroutine firingCoroutine;

private void Awake()
{
    audioPlayer = FindObjectOfType<LD_AudioPlayer>();
}

void Start()
{
    if(useAI)
    {
        isFiring = true;
    }
}

void Update()
{
    Fire();
}

void Fire()
{
    if(isFiring && firingCoroutine == null)
    {
        firingCoroutine = StartCoroutine(FireContinuously());
    }
    else if(!isFiring && firingCoroutine != null)
    {
        StopCoroutine(firingCoroutine);
        firingCoroutine = null;
    }
}

IEnumerator FireContinuously()
{
    while(true)
    {
        GameObject instance = Instantiate(projectilePrefab, transform.position, Quaternion.identity);

        Rigidbody2D rb = instance.GetComponent<Rigidbody2D>();
        if (rb != null)
        {
            rb.velocity = transform.up * projectileSpeed;
        }

        Destroy(instance, projectileLifetime);

        audioPlayer.PlayShootingClip();
        //audioPlayer.GetInstance().PlayShootingClip();

        //float timeToNextProjectile = Random.Range(baseFiringRate - firingRateVariance, baseFiringRate + firingRatePlayer Variance);
        //timeToNextProjectile = Mathf.Clamp(timeToNextProjectile, minimumFiringRate, float.MaxValue);

        //yield return new WaitForSeconds(timeToNextProjectile);

        if (useAI)
        {
            firingRateAI = Random.Range(firingRateAiMax, firingRateAiMin);
            yield return new WaitForSeconds(firingRateAI);
        }
        else
        {
            yield return new WaitForSeconds(firingRatePlayer);
        }
    }
}

}

Maybe someone knows a answer for my problem.

Thanks in advance

Daniel :slight_smile:

1 Like

You have an infinite while loop.

while (true) 
{
 // ...
}

Unless you have a variable or expression that will eventually evaluate to false the while loop will never exit.

try something like this

bool canFire = true;

while (canFire) 
{
  /* do some stuff until it should no longer be done */
    canFire = false;
}
1 Like

Thanks for your Idea normally i would made my script like this but after i took a additonal look on my complete project i found the problem, that the new input System (OnFire) was set on Button instend of value.
:man_facepalming:

The Problem is now fixed.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms