Instantiating enemies at a 90 degree angle?

Hi! I am creating the “Laser Defender” game from the Unity 2D course. I was attempting to add some personal flare to the project by having enemies come from the top of the screen while facing sideways and shooting from the sides of the screen. However, I am unsure of how to accomplish this. Here is the script for spawning enemies:

IEnumerator SpawnEnemyWaves()
{
do
{
foreach(WaveConfigSO wave in waveConfigs)
{
currentWave = wave;
for(int i = 0; i < currentWave.GetEnemyCount(); i++)
{
Instantiate(currentWave.GetEnemyPrefab(i),
currentWave.GetStartingWayPoint().position,
Quaternion.Euler (0, 0, 180), transform);
yield return new WaitForSeconds(currentWave.GetRandomSpawnTime());
}
yield return new WaitForSeconds(timeBetweenWaves);

    }
    
    }
    
    while(isLooping);
}

I was wondering if I could somehow attach a public float containing a “rotationPosition” onto the enemies, and then pass that variable into the Z axis parameter within the Euler? I tried multiple times but I kep coming up with an error along the lines of “rotationPosition doesn’t exist within the current context”. I was wondering if someone could help me out with this, or perhaps direct me to an appropriate place to ask. Thank you!

Sure we can help you, but I am a little unsure about what exactly you are asking.

Quaternion.Euler(0, 0, 180) here is used to rotate the enemies 180° so that they face down. To make them face sideway, all you need is to change that 180 to either 90 or -90 (or 270).

You could configure this by adding the value to the WaveConfigSO. That would then be the value for each respective wave.

// In WaveConfigSO
[SerializeField] float spawnAngle = 180f;

public float GetSpawnAngle()
{
    return spawnAngle;
}

And then in the spawn for loop

for (int i = 0; i < currentWave.GetEnemyCount(); i++)
{
    Instantiate(currentWave.GetEnemyPrefab(i),
                currentWave.GetStartingWaypoint().position,
                Quaternion.Euler(0,0,currentWave.GetSpawnAngle()),
                transform);
    yield return new WaitForSeconds(currentWave.GetRandomSpawnTime());
}

Is this what you wanted?

Hi! I appreciate the response! That makes sense to me an I appreciate it! That said, my issue is that I am trying to make different enemy types come in at different angles. For example, green enemies could come in facing down while purple enemies could come in facing left.

My thought process has been to prefab different enemy types, each with different information attached that would change the angle at which they spawn. I was thinking that attaching a public float script to them that could contain this information that could then be used in the spawn for loop. I was trying to do it basically how you just did it, except I kept getting the previously mentioned error message. I was wondering if someone could help me with the formatting (or let me know if it is possible in the first place).

Either way, I really appreciate your input, and I like the utility of putting it on the WaveConfigSO. Let me know if you can help me do it the other way or not. Thank you very much!

You can do exactly that.

Let’s say you attach a script to your prefab called SpawnConfig.cs

public class SpawnConfig : MonoBehaviour
{
    public float SpawnAngle = 90f;
}

then, when you want to spawn it, you can get that angle from the prefab

for (int i = 0; i < currentWave.GetEnemyCount(); i++)
{
    GameObject enemyPrefab = currentWave.GetEnemyPrefab(i);
    SpawnConfig spawnConfig = enemyPrefab.GetComponentInChildren<SpawnConfig>();
    Instantiate(enemyPrefab,
                currentWave.GetStartingWaypoint().position,
                Quaternion.Euler(0,0,spawnConfig.SpawnAngle),
                transform);
    yield return new WaitForSeconds(currentWave.GetRandomSpawnTime());
}

Note: This doesn’t check if the spawnConfig is null, so you need to make sure it isn’t before using it

You can now configure the SpawnAngle for each prefab in the inspector

1 Like

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

Privacy & Terms