Forum begins after the advertisement:


[Part 7] Enemies are not spawning in correct positions

Home Forums Video Game Tutorial Series Creating a Rogue-like Shoot-em Up in Unity [Part 7] Enemies are not spawning in correct positions

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #14890
    Jason Hahne
    Participant

    For relativeSpawnpoints, I have 8 points around the player. However the enemies only spawn on top, bottom, right, and left side. They don’t spawn on points that are located diagonal to the player.
    Foreach loop inside the spawnEnemies:

    foreach (var enemyGroup in waves[currentWaveCount].enemyGroups)
    {
        while (waves[currentWaveCount].spawnCount < waves[currentWaveCount].waveQuota)
        {
            if (enemiesAlive >= maxEnemiesAllowed)
            {
                maxEnemiesReached = true;
                return;
            }
            Instantiate(enemyGroup.enemyPrefab, player.position + relativeSpawnPoints[Random.Range(0, relativeSpawnPoints.Count)].position, Quaternion.identity);
    
            enemyGroup.spawnCount++;
            waves[currentWaveCount].spawnCount++;
            enemiesAlive++;
        }
    }
    #14891
    Terence
    Keymaster

    Hi Jason, you can modify your code like this to print out the spawn point that is being used every time an enemy is spawned. This should give you insight into why the issue is occurring:

    foreach (var enemyGroup in waves[currentWaveCount].enemyGroups)
    {
        while (waves[currentWaveCount].spawnCount < waves[currentWaveCount].waveQuota)
        {
            if (enemiesAlive >= maxEnemiesAllowed)
            {
                maxEnemiesReached = true;
                return;
            }
    
            int selectedIdx = Random.Range(0, relativeSpawnPoints.Count);
            Instantiate(enemyGroup.enemyPrefab, player.position + relativeSpawnPoints[selectedIdx].position, Quaternion.identity);
            print("Selected index: " + selectedIdx + "; spawn point used: " + relativeSpawnPoints[selectedIdx].name);
            Instantiate(enemyGroup.enemyPrefab, player.position + relativeSpawnPoints[Random.Range(0, relativeSpawnPoints.Count)].position, Quaternion.identity);
    
            enemyGroup.spawnCount++;
            waves[currentWaveCount].spawnCount++;
            enemiesAlive++;
        }
    }
    #14893
    Jason Hahne
    Participant

    I found out why the issue was happening, the points that were placed diagonally were placed farther than despawnDistance.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: