Could we use Coroutines too ? To make the enemy shoot?

Hi. I have a doubt : could we also use Coroutines to make the enemy shoot ? For example I have added the following coroutine in my project. Is it ok ?

void Start()
{
StartCoroutine(Shoot(laserPrefab));
shotCounter = UnityEngine.Random.Range(minTimeBetweenShots,maxTimeBetweenShots);
}

IEnumerator Shoot(GameObject laserPrefab)
{
    while(true)
    {
        GameObject laser = Instantiate(laserPrefab, transform.position, Quaternion.identity);
        laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, -projectileSpeed);
        yield return new WaitForSeconds(shotCounter);
        shotCounter = UnityEngine.Random.Range(minTimeBetweenShots,maxTimeBetweenShots);
    }
}

And if my method is right, why didn’t you use coroutine ? BY the way my methdo is working just fine

Hi Maksimjeet,

Good job on developing your own solution. Does it work? If so, it is fine. :slight_smile:

Rick wants to teach a variety of techniques in his course. That’s why most “why not” questions can be answered with “because it does not work” or “because Rick cannot show every solution there is”.

Keep up the good work!

1 Like

Understood. Thanks a lot ! Will keep it in mind !

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms