所以我是C#中的一名新程序員(我知道)我試圖在Unity中創建一個實時編碼系統的原型,但只是聲音和儀表在統一。Unity中的重裝原型不會正確減少子彈和子彈
問題是,當我重新加載它不會正確減少彈藥。因爲我這樣做是爲了在重新加載時仍然有子彈離開時,它只會從彈丸中減去彈藥,我已經必須實施,它將只減去一個如果(鏡頭== 0)
I while我遇到了一些問題(另一個令人震驚),並且看到我的整個程序只是if()語句。重寫和重構後,我仍然有我的問題,如果發言。我被告知不要發佈200行代碼並且具體。
我要發佈200行代碼,因爲我不知道更好。我很抱歉。
公共類射擊:MonoBehaviour {
//"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;
}
}
}
這裏沒有清楚的問題。你所說的只是你有問題。 – Kramb
我建議將此改爲[Code Review SE](https://codereview.stackexchange.com/)。我在'GetAudio()'中看到的一件事是傳遞的索引總是比使用的索引高1。 'return audio [index-1];'會工作,將這些if塊中的所有*塊合併爲一個語句(儘管如此,仍然需要檢查是否出界)。 – Draco18s
你希望我們能幫助解決什麼問題? –