首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Unity中重新加载原型并不能正确减少弹药和子弹

在Unity中重新加载原型并不能正确减少弹药和子弹
EN

Stack Overflow用户
提问于 2017-07-15 00:25:15
回答 2查看 695关注 0票数 -2

所以我是一个新的C#程序员(我知道是震撼的),我试着在Unity中创建一个重播系统的原型,但只是在Unity中使用声音和仪表。

问题是,当我重新装弹时,它不能正确地减少弹药。因为我这样做了,当你仍然有子弹的时候重新装弹时,它会从射击中减去弹药,我已经实现了它只会减去一个if (镜头== 0)。

在我遇到一些问题(另一个令人震惊的问题)后,我发现我的整个程序都是if()语句。在重写和重构之后,我仍然有我的问题和if语句。我被告知不要张贴200行代码,而且要具体。

我要发布200行代码,因为我不知道更好的。真抱歉。

公共类快照: MonoBehaviour {

代码语言:javascript
复制
//"CoolDown" is used to limit rate of fire of my gun
public int CoolDown = 5;

//used to prevent shooting while realoding
public int ReloadCoolDown;
public bool IsReloading = false;

//obvious
public int Shots = 0;
public int TotalShots;
public int Magazine = 25;
public int Ammo = 125;
public bool NoAmmo = false;



void Start() 
{
    ReloadCoolDown = 150;
}

void Update()
{
    //Checks if ammo is still present
    CheckForAmmo();
    //just so i could test some stuff faster, can be ignored
    Skip();
    //Also checks for ammo
    if(!NoAmmo)
    {
        GameShot();
        ReloadEmpty();
        ReloadHalf();
        if (IsReloading == true)
        {
            ReloadCoolDown--;
        }
        if (CoolDown <= 0)
        {
            CoolDown = 0;
        }
        CoolDown--;
    }
    else if (NoAmmo)
    {
        ExecNoAmo();
    }
}

//Just getting the audio clips from unity
AudioSource GetAudio(int index)
{
   AudioSource[] audio = GetComponents<AudioSource>();
    if (index == 1)
    {
        return audio[0];
    }
    else if (index == 2)
    {
        return audio[1];
    }
    else if (index == 3)
    {
        return audio[2];
    }
    else if (index == 4)
    {
        return audio[3];
    }
    else
        return null;


}

void GameShot()
{
    //Shoots, increases total shots and shots (for that mag), plays audio, sets the cooldown for the next shot, decreases bullets in mag
    if (Input.GetKey(KeyCode.Space) &&
        CoolDown <= 0 && IsReloading == false)
    {
        TotalShots++;
        GetAudio(1).Play();
        CoolDown = 5;
        Shots++;
        Magazine--;
    }
}

//Reloads if every bullet in the magazine has been fired
void ReloadEmpty()
{
    //this and ReloadHalf() is where you can find so many if statements and where most of my code is tangled up...
    //im trying to check for ammo and if the mag is completely empty to trigger the empty reload
    if (Magazine == 0 && Ammo > 0)
    {

        if(Ammo >= 25)
        {
            Magazine = 25;
        }
        else
        {
            Magazine = Ammo; 
        }

        Ammo -= Shots;
        Shots = 0;
        ReloadCoolDown = 130;
        GetAudio(2).Play();

        IsReloading = true;
    }
    if (ReloadCoolDown <= 0)
    {
        ReloadCoolDown = 150;
        IsReloading = false;
    }
}
void ReloadHalf()
{
    //Again, many if statements and entaglement...
    if ((Input.GetKeyDown(KeyCode.R) && Magazine < 26) && Ammo > 0)
    {
        if (Shots == 0)
            Ammo -= 1;
        ReloadCoolDown = 80;
        GetAudio(3).Play();
        if(Ammo >= 25)
        { 
            Magazine = 26;
            Ammo -= Shots;
        }
        else if (Ammo <= 25)
        {
            Magazine += Ammo;
            if(Magazine > 26)
            {
                int i = Magazine - 25;
                Ammo = i;
                Magazine = 26;
            }

        }
        Shots = 0;
        IsReloading = true;
    }
    if (ReloadCoolDown <= 0)
    {
        ReloadCoolDown = 100;
        IsReloading = false;
    }
}

void ExecNoAmo()
{
    //plays no ammo sound if ammo == 0
    if(Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.R))
        GetAudio(4).Play();

}

void CheckForAmmo()
{
    if (Ammo <= 0 && Magazine <= 0)
        NoAmmo = true;
}

void Skip()
{
    if (Input.GetKeyDown(KeyCode.Z))
    {
        Ammo = 25;
    }
}

}

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45107667

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档