Enemy Laser spawns multiple times

Hi all. Really loving this section of the course as this style of game takes me back to my youth. However I’m running into a small problem. My enemy lasers spawn multiple times on top of each other, so that it looks like it’s a solid wall of laser. I’ve tried playing with the values for the shot counter, however that didn’t work. I’ve also verified that only the damage dealer script is on the projectiles, and only the enemy pathing and enemy scripts are on the enemies. I’ve included my enemy.cs code below. Any help would be greatly appreciated!

[SerializeField] float health = 100f;
    [SerializeField] float shotCounter;
    [SerializeField] float minTimeBetweenShots = 0.2f;
    [SerializeField] float maxTimeBetweenShots = 3f;
    [SerializeField] float projectileSpeed = 2f;
    [SerializeField] GameObject enemyProjectile;
    // Start is called before the first frame update
    void Start()
    {
        shotCounter = Random.Range(minTimeBetweenShots, maxTimeBetweenShots);
    }

    // Update is called once per frame
    void Update()
    {
        EnemyShot();
    }

    private void EnemyShot()
    {
        shotCounter -= Time.deltaTime;
        if(shotCounter <= 0f)
        {
            Fire();
        }
    }

    private void Fire()
    {
        GameObject enemyWeapon = Instantiate(enemyProjectile, transform.position, Quaternion.identity) as GameObject;
        enemyWeapon.GetComponent<Rigidbody2D>().velocity = new Vector2(0, -projectileSpeed);
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        DamageDealer damageDealer = other.gameObject.GetComponent<DamageDealer>();
        if (!damageDealer) { return; }
        ProcessHit(damageDealer);
    }

    private void ProcessHit(DamageDealer damageDealer)
    {
        health -= damageDealer.GetDamage();
        damageDealer.Hit();
        if (health <= 0)
        {
            Destroy(gameObject);
        }
    }
}

It’s because your shot counter never gets reset once it reaches 0 so after that it’s always less than 0 and every frame it keeps spawning new lasers

Thanks! I knew it had to be something simple. I just wasn’t seeing it. I appreciate the help :smiley:

No problem! Glad it’s worked out for you!

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

Privacy & Terms