c# - 尝试在 c# 中进行重新加载延迟但遇到错误
问题描述
所以我最近一直在尝试制作游戏,但我遇到了一个意想不到的错误。
当我运行我编写的脚本时,我可以从我的枪中射出 10 发子弹,然后它会播放重新加载声音并使用协程等待两秒钟,它会无缘无故地向我的剪辑添加 1200?所以我添加了 if 语句,告诉它如果剪辑大小超过 10,则将其恢复为 10,但现在它会在几秒钟内变为 19,然后又回到 10。
我不知道我是不是白痴还是什么,但很高兴得到一些帮助,如果这是重复的,我也很抱歉,但我在 c# 中找不到类似的东西。
编辑:我现在已经修复了这个错误,感谢您的帮助,在旧代码之后分享新代码以供将来参考!
我的旧代码:
{
public float fireRate = 10;
public float damage = 15;
public int clipsize;
float timeUntilFire = 0;
public float ReloadSpeed = 2.5f;
float reloadtime;
public Transform firePoint;
public GameObject bullet;
public AudioSource gunshotaudio;
public AudioSource ReloadSound;
void Awake()
{
gunshotaudio = GetComponent<AudioSource>();
clipsize = 10;
}
// Update is called once per frame
void Update()
{
// if the button pressed is fire 1 and
if (Input.GetButtonDown("Fire1") && clipsize != 0)
{
timeUntilFire = Time.time / fireRate;
gunshotaudio.Play();
Shoot();
clipsize -= 1;
Debug.Log(clipsize);
}
if(clipsize <= 0)
{
ReloadSound.Play();
StartCoroutine(Wait());
StopCoroutine(Wait());
}
if(clipsize >= 11)
{
clipsize = 10;
}
}
IEnumerator Wait()
{
yield return new WaitForSecondsRealtime(2);
clipsize += 10;
}
void Shoot()
{
gunshotaudio.Play();
Instantiate(bullet, firePoint.position,firePoint.rotation);
}
} ** 我的新代码:**
{
public float fireRate = 10;
public float damage = 15;
public int clipsize;
float timeUntilFire = 0;
public float ReloadSpeed = 2.5f;
float reloadtime;
bool CanShoot;
bool IsReloading;
bool reloading;
public Transform firePoint;
public GameObject bullet;
public AudioSource gunshotaudio;
public AudioSource ReloadSound;
void Awake()
{
gunshotaudio = GetComponent<AudioSource>();
clipsize = 10;
}
void Start()
{
if (clipsize > 0)
{
CanShoot = true;
}
}
// Update is called once per frame
void Update()
{
// if the button pressed is fire 1 and
if (Input.GetButtonDown("Fire1") && clipsize != 0)
{
timeUntilFire = Time.time + fireRate;
gunshotaudio.Play();
Shoot();
clipsize--;
Debug.Log(clipsize);
}
// reloading
if(Input.GetKeyDown(KeyCode.R))
{
IsReloading = true;
ReloadSound.Play();
}
if (IsReloading)
{
StartCoroutine(Wait());
}
}
IEnumerator Wait()
{
if(reloading == false)
{
if (IsReloading)
{
reloading = true;
CanShoot = false;
yield return new WaitForSeconds(1);
clipsize = 10;
IsReloading = false;
Debug.Log(clipsize);
CanShoot = true;
reloading = false;
}
else
{
}
}
}
void Shoot()
{
gunshotaudio.Play();
Instantiate(bullet, firePoint.position,firePoint.rotation);
}
}
解决方案
您没有检查重新加载是否已经在进行中,因此您正在排队一堆重新加载事件,这些事件都将 +10 添加到剪辑中,从而产生额外内容。
添加一个reloading
布尔值并在启动协程之前检查它,而不是向剪辑添加 10,而是将剪辑设置为 10。
推荐阅读
- php - 参数 1 传递给 Symfony\Component\VarDumper\Server\Connection::__construct() ... 在第 49 行的 DumpServerServiceProvider.php 中调用
- c# - 在 .NET 中,什么时候调用 getter 和 setter?
- python - 重组 pandas 数据框,将 Column 变成新的 Header,原始 Header 成为具有预先存在的 Column 的多索引的一部分
- angular - 如何与重定向共享令牌验证功能?
- django - 没有将其注册为应用程序的 Django 文件夹
- kubernetes - Cassandra Kubernetes Statefulset NoHostAvailableException
- android - 如何从pdf阅读器的文件中获取pdf
- reactjs - 如何在 Azure 中安装 Semantic-UI
- reactjs - FontAwesome 微调器不会在 React 中旋转
- java - 模拟服务层中的字段注入依赖项,包括推土机映射器