Hello guys! I was following the lesson step by step and I found this problem, when I Get my button up the coroutine doesnt stop. I tried to place a Debug.Log to see if it didnt entered the if stamente but it does. Here’s the two options of coding I tried:
1—
void Fire()
{
if (Input.GetButtonDown("Fire1"))
{
StartCoroutine(FireContinuously());
}
if (Input.GetButtonUp("Fire1"))
{
StopCoroutine(FireContinuously());
Debug.Log("Paramos");
}
}
IEnumerator FireContinuously()
{
while (true)
{
GameObject laser = Instantiate(laserPrefab, transform.position, Quaternion.identity) as GameObject;
laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
yield return new WaitForSeconds(projectileFiringPeriod);
}
}
2----
void Fire()
{
if (Input.GetButtonDown("Fire1"))
{
firing = StartCoroutine(FireContinuously());
}
if (Input.GetButtonUp("Fire1"))
{
StopCoroutine(firing);
Debug.Log("Paramos");
}
}
IEnumerator FireContinuously()
{
while (true)
{
GameObject laser = Instantiate(laserPrefab, transform.position, Quaternion.identity) as GameObject;
laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
yield return new WaitForSeconds(projectileFiringPeriod);
}
}