简体   繁体   中英

Unity C# - Reloading Script not working correctly

So this script should switch through magazines, not fill a magazine. What happens is with the first reload, even tho' the animation happens and everything. The magazine doesn't change until after you shoot and then reload. Reloading again will switch between 2 out of 4 magazines even if the 2 are empty (which is expected because it can switch if there is a magazine with bullets).

Here is the code:

if (Input.GetAxis ("Reload") > 0 && reloading == false && pressedReload == false && runningAutomatic == false && mags[currMag] < magazineSize && animator.GetCurrentAnimatorStateInfo (0).IsName (shootAnim.name) == false) {
    for (int i = 0; i < mags.Length; i++) {

        if (mags [i] > 0) {
            Reload (currMag + 1);
            animator.SetFloat ("ReloadSpeed", reloadSpeed);
            animator.Play (reloadAnim.name);
            pressedReload = true;
            reloading = true;
        }
    }
}
reloading = animator.GetCurrentAnimatorStateInfo (0).IsName (reloadAnim.name);
    if (reloading)
        pressedReload = false;

...

void Reload (int currentMagazine) {

    if (currentMagazine == mags.Length)
    currentMagazine = 0;
    currMag = currentMagazine;
}

Just forgot to break; the for loop... Thanks to Serlite :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM