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);
}
}
}