【发布时间】:2017-12-19 19:58:30
【问题描述】:
所以我是 C# 的新程序员(我知道这很令人震惊),我尝试在 Unity 中制作一个重新编码系统的原型,但只是在 Unity 中使用声音和仪表。
问题是当我重新加载时,它并没有正确减少弹药。因为我这样做是为了在你还剩下子弹的情况下重新加载时,它只会从子弹中减去弹药,我已经必须实现它只会减去一个 if (shots == 0)
在我遇到一些问题(另一个令人震惊)并看到我的整个程序只是 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;
}
}
}
【问题讨论】:
-
这里没有明确的问题。你所说的只是你遇到了问题。
-
我建议将其发送给Code Review SE。我在
GetAudio()中看到的一件事是传递的索引总是比使用的索引高 1。return audio[index-1];可以工作,将 所有 的 if 块合并到一个语句中(不过,您仍然需要检查是否越界)。 -
您希望我们帮助解决什么问题?
-
@Draco18s 我的问题是,当您在低弹药时重新加载时,该代码无法正确计算射击次数。对不起,我不应该说得更清楚。我的问题也不是 GetAudio() 方法。这是重新加载方法。我无法在 Code Review SE 上发布我的代码,因为我的代码不起作用。我是这方面的新手,所以我不太确定如何问我的问题。下次我会更具体。现在重写我的代码。 :D
-
编辑您的问题以包含问题陈述(在您的代码块之外!没有人会注意到 cmets 直到他们开始寻找所陈述的问题)。