Hey. There is such a code -

if (Input.GetButtonDown("Reload") && !InRealod && Ammo < MaxAmmo) { StartCoroutine(ReloadCountdown()); } if (InRealod && Input.GetButtonDown("Fire") && Ammo != 0) { StopCoroutine(ReloadCountdown()); } IEnumerator ReloadCountdown() { while (Ammo < MaxAmmo) { InRealod = true; yield return new WaitForSeconds(ReloadTime); Ammo++; InRealod = false; } } 

But Korutina is not interrupted. All conditions are met. But she interrupts herself when Ammo == MaxAmmo. Even if you do this -

 IEnumerator ReloadCountdown() { while (Ammo < MaxAmmo && !Input.GetButtonDown("Fire")) { InRealod = true; yield return new WaitForSeconds(ReloadTime); Ammo++; InRealod = false; } } 

nothing works. The idea is that charging cartridges into a weapon should go until the player presses the fire button (if Ammo! = 0). Why is it impossible to interrupt corutin?

    1 answer 1

    In the line StopCoroutine (ReloadCountdown ()) - in fact, you first start a new reloading quotation level, and then stop it. You need to cache the quortenine that is returned by StartCoroutine (), and pass the link to it to the StopCoroutine method. In addition, the verification of the reference to recharge corutin can be used instead of the flag InRealod. Below is the implementation code.

    And in the second case, your problem is related to the fact that not every frame is checked for input. When you call yield return new WaitForSeconds (ReloadTime), the quorutine is suspended for the duration of this timer and then continues to run. Those. she won't know whether you pressed the button or not while she waited.

      Coroutine _reloadRoutine; void Update() { if (Input.GetButtonDown("Reload") && _reloadRoutine == null && Ammo < MaxAmmo) { _reloadRoutine = StartCoroutine(ReloadCountdown()); } if (_reloadRoutine != null && Input.GetButtonDown("Fire") && Ammo != 0) { StopCoroutine(_reloadRoutine); _reloadRoutine = null; } } IEnumerator ReloadCountdown() { while (Ammo < MaxAmmo) { InRealod = true; yield return new WaitForSeconds(ReloadTime); Ammo++; InRealod = false; } _reloadRoutine = null; } 
    • Thank you very much. Another question: how to make the charge not interrupted before the cartridge goes into the chamber? That is, the process of charging a single bullet is in progress -> Fire is pressed and as soon as the bullet stops in the chamber of the quorutine, stop? And then if you add a reload animation, problems may begin. For example, there is a reload animation, the player presses Fire, the bullet (in the code) is already in the chamber, and in the animation he only gets it. - xomem
    • @xomem, well, I see the solution through the addition of an animation of charging a bullet. Unity has a StateMachineBehaviour class - it, like its heirs, can be hung inside the animator and subscribe to its events. He also reports when an animator exits a certain state by getting the hash of the state into the method. As an option - a bullet in the chamber should be counted exactly when the exit from the state of "charge" occurs. And when you press the shot, do not stop the coruntine, but set the interrupt flag. And if at the exit from the state "reloading" the flag is worth - to interrupt the corutin. - M. Green
    • thanks again - homeome