U
U
Uncle Bogdan2021-04-09 16:36:39
Unity
Uncle Bogdan, 2021-04-09 16:36:39

What is the correct way to write this?

I'm wondering how to properly write wait until next shot?
After the shot, he must wait one second and again there is an opportunity to shoot, but how to write it correctly?
My decision:

private void Update()
    {
        if (Input.GetButtonDown("Fire1") && CanFire)
        {
            GameObject Bullet = Instantiate(BulletPrefab, FirePoint.position, transform.rotation);
            Bullet.GetComponent<Rigidbody2D>().AddForce(transform.up * BulletSpeed);
            CanFire = false;
            Invoke("FireRateFunc", FireRate);
        }
    }

    private void FireRateFunc()
    {
        CanFire = true;
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Zaletsky, 2021-04-09
@JZ_52

IEnumerator ExecuteAfterTime(float timeInSec)
{
yield return new WaitForSeconds(timeInSec);
// do the right thing
}
I think you need this function

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question