Two enemies spawn on top of each other and a total of 6 in the wave

So I am having a weird issue right now where I see this:


In the photo, you can also see what my enemy has on it.
And here is the wave:
2019-08-16%20(3)
Here is the wave spawner script:

    [SerializeField] GameObject enemyPrefab;
    [SerializeField] GameObject pathPrefab;
    [SerializeField] float timeBetweenSpawns = 0.5f;
    [SerializeField] float spawnRandomFactor = 0.3f;
    [SerializeField] int numberOfEnemies = 5;
    [SerializeField] float moveSpeedForWave = 2f;

    // The get methods for each of the serialize fields above
    public GameObject GetEnemyPrefab() { return enemyPrefab; }

    public List<Transform> GetWaypoints()
    {
        var waveWaypoints = new List<Transform>();
        foreach (Transform child in pathPrefab.transform)
        {
            waveWaypoints.Add(child);
        }
        return waveWaypoints;
    }

    public float GetTimeBetweenSpawns() { return timeBetweenSpawns; }

    public float GetSpawnRandomFactor() { return spawnRandomFactor; }

    public int GetNumberOfEnemies() { return numberOfEnemies; }

    public float GetMoveSpeedForWave() { return moveSpeedForWave; }
    // end the get methods

The enemy pathing:

   [SerializeField] WaveConfig waveConfig;

    // for the wayponts
    List<Transform> waypoints;

    // for moving the enemies
    [SerializeField] float movingSpeed = 2f;
    int waypointIndex = 0;

    // Start is called before the first frame update
    void Start()
    {
        waypoints = waveConfig.GetWaypoints();
        transform.position = waypoints[waypointIndex].transform.position;
    }

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

    private void Move()
    {
        // to move the position
        if (waypointIndex <= waypoints.Count - 1)
        {
            var targetPosition = waypoints[waypointIndex].transform.position;
            var movementThisFrame = movingSpeed * Time.deltaTime;
            transform.position = Vector2.MoveTowards
                (transform.position, targetPosition, movementThisFrame);

            // if at the last waypoint
            if (transform.position == targetPosition)
            {
                waypointIndex++;
            }
        }
        // no vaild waypoint
        else
        {
            Destroy(gameObject);
        }
    }

And finally the enemy spawner

    [SerializeField] List<WaveConfig> waveConfigs;

    // to start the waves
    int startingWave = 0;

    // Use this for initialization
    void Start()
    {
        var currentWave = waveConfigs[startingWave];
        StartCoroutine(SpawnAllEnemiesInWave(currentWave));
    }

    private IEnumerator SpawnAllEnemiesInWave(WaveConfig waveConfig)
    {
        for (int enemyCount = 0; 
             enemyCount < waveConfig.GetNumberOfEnemies();
            enemyCount++)
        {
            Instantiate(
                waveConfig.GetEnemyPrefab(),
                waveConfig.GetWaypoints()[0].transform.position,
                Quaternion.identity);
            yield return new WaitForSeconds(waveConfig.GetTimeBetweenSpawns());
        }
    }

Any help on sorting the double enemy at the start of the wave would be greatly appciated.

On a slightly different note on the

        for (int enemyCount = 0; 
            enemyCount < waveConfig.GetNumberOfEnemies();
            enemyCount++)

Why does it work with enemyCount < waveConfig.GetNumberOfEnemies() and not enemyCount <= waveConfig.GetNumberOfEnemies()? I thought it would be the <= and that is how I did it until I saw how Rick did it.

Thank you in advance!!

Hi,

Have you already tried to increase the time between the spawns? 0.5 seconds is very fast. For testing purposes, I’d increase it to see if the value affects the first two enemies. If it does not, it might be that there is no delay between the spawning of the first enemy and the spawning of the second one.

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?

1 Like

I think it was the spawn rate combined with the enemy I have on the screen just for testing was bugging out. Thanks for the advice!!
On another note, I just saw that my enemies are spawning facing the wrong way. They are spawning facing up instead of down despite the fact my prefab is facing down (you can see how it is spawned in my first picture). I followed how Rick did it exactly but mine for some reason flipped.

Try to set the z-rotation to 0.

Tried setting the rotation of the enemies to 0 in the prefabs and still have them spawning upside down. Maybe if I delete the prefabs and then recreate them again it will have them spawn correctly?

I think I ran into something similar in my block breaker. What I had done was dragged an instanced prefab that I had changed into my script that was instantiating the prefab. So it was instantiating that instance and not the actual prefab. Maybe have your script set the prefab, or drag a new prefab into the scene that has the rotation right then drag that into your prefab that is creating the enemies. Just an idea.

The other idea is to use the Quaternion to rotate the object by 180 degrees. I think there is an Quaternion.Euler function that would let you rotate instead of using Quaternion.Identity.

Quaternion rotation = Quaternion.Euler(0, 90, 0);

1 Like

Thanks for the suggestion @mbalrog6!! I was able to get it working through clicking the flip y-axis button on the prefabs. I for some reason did not think of this but after reading your comment I went and had a look at how my prefabs were set up.

I was looking at your second suggestion but I could not figure out how to put your line of code into my code. Here is what I have tried the EnemySpawner:

   private IEnumerator SpawnAllEnemiesInWave(WaveConfig waveConfig)
    {
        Quaternion rotation = Quaternion.Euler(0, 90, 0);
        for (int enemyCount = 0; enemyCount < waveConfig.GetNumberOfEnemies(); enemyCount++)
        {
            var newEnemy = Instantiate(
                waveConfig.GetEnemyPrefab(),
                waveConfig.GetWaypoints()[0].transform.position,
                rotation);
            newEnemy.GetComponent<EnemyPathing>().SetWaveConfig(waveConfig);
            yield return new WaitForSeconds(waveConfig.GetTimeBetweenSpawns());
        }
    }

Sadly this just makes all of the waves nonextisent. Looking over all of the rest of my code I cannot think of another spot that would effect how the enemies might spawn.

What do you mean by that? Aren’t the enemies in the Hierarchy? Maybe they are just rotated around the wrong axis.

Alright after looking at it again with fresh eyes I realized I was rotating it around the y-axis when I need it to be around the z-axis.
Here is the code that would be used to rotate it if you did not want to click the flip axis in the inspector.

    private IEnumerator SpawnAllEnemiesInWave(WaveConfig waveConfig)
    {
        Quaternion rotation = Quaternion.Euler(0, 0, 180);
        for (int enemyCount = 0; enemyCount < waveConfig.GetNumberOfEnemies(); enemyCount++)
        {
            var newEnemy = Instantiate(
                waveConfig.GetEnemyPrefab(),
                waveConfig.GetWaypoints()[0].transform.position,
                rotation);
            newEnemy.GetComponent<EnemyPathing>().SetWaveConfig(waveConfig);
            yield return new WaitForSeconds(waveConfig.GetTimeBetweenSpawns());
        }
    }

Thank you @Nina and @mbalrog6!! You guys really saved this project’s visuals.

You’re welcome. :slight_smile:


See also:

1 Like

Forgot to marked as solved but just did thank you!!

Your welcome. I am glad I was able to help.

1 Like

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

Privacy & Terms