试图在C#中进行重新加载延迟,但仍遇到错误

时间:2019-02-26 21:27:15

标签: c# unity3d coroutine

所以我最近一直在尝试制作游戏,但遇到了一个意想不到的错误。

运行编写的脚本时,我可以从枪中射出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);
}

}

1 个答案:

答案 0 :(得分:1)

您不是要检查是否正在执行重新加载,因此您正在排队一堆重新加载事件,这些事件都会使剪辑加+10,从而产生额外的结果。

添加一个reloading布尔值,并在启动协程之前检查一下,而不是在剪辑中添加10,而是将剪辑设置为10。