私はC#の新しいプログラマーです(ショッカー、私は知っています)。Unityでの再現システムを試作しようとしましたが、統一Unityでプロトタイプをリロードしても弾薬と弾丸が正確に減少しない
問題は、リロードしても弾薬が正しく減少しないということです。私はそれを作ったので、まだ弾丸が残っている時にリロードすると、ショットから弾薬を減算するだけで、すでに1つ減算することを実装しなければならなかったのです(0120)。私はいくつかの問題(別のショッカー)に遭遇し、私の全体のプログラムがif文であることを見た。書き直しとリファクタリングの後、私はまだ問題と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
これを代わりに[コードレビューSE](https://codereview.stackexchange.com/)にすることをお勧めします。私が 'GetAudio()'で見たことの一つは、渡されたインデックスが常に使用されているインデックスより1高いことです。 'return audio [index-1];'は、ifブロックの* all *を1つのステートメントに畳ませるように動作します(ただし、範囲外を確認する必要があります)。 – Draco18s
あなたはどのような問題を解決することができますか? –