Uploaded October 2020 | Updated September 2026, 3 weeks ago
#AmongUs #UnitySpawn #UnityRandomSpawn
Episode 6. Spawn Enemies At Random Position With Random Color In Among Us 2D Shooter Clone Made With Unity
I play games here - twitch.tv/zzotoff
You are very welcome :-)
Previous Video
Episode 5. Make Enemy Follow The Player In 2D Top Down Game Like Among Us Shooter Clone Made With Unity youtu.be/jdAoLlCn2e8
Hi!
In this video I show you a simple script that allows you to spawn enemies at random position with random color. Once spawned an enemy starts moving towards the player. This feature was created in a previous video. Link is in the description. So here is my project from that previous video. Player is blue. Enemy is pink. Nothing is new here except this script named Enemy spawner. This script will be attached to Enemy spawner game object which I’m creating right away. There we go. Here it is right in the middle. I take this script and D&D it into the enemy spawner game object. Once script is attached I have to put a game object to be spawned into this slot. If I want to instantiate a game object then I need a prefab of it. Since I want to spawn enemies I create an enemy prefab by D&Ding enemy game object into prefabs folder. There we go. Prefab is created. Now I can delete this enemy from the hierarchy. And once a prefab is created I can D&D it into enemy spawner script component here. Ok. Now it’s time to take a look how this script works. Here it is.
Variables, variables, variables. And in Start method I begin to invoke Spawn New Enemy method every two seconds. What’s inside this method? First of all I randomly choose at which area a new enemy will be spawned. Let me show you. There are four zones or areas where I will be spawning enemies. One area is about here. Second one is about here. Third one i about here. And forth one is about here. So a new enemy will be spawned outside the play zone and will never be spawned inside it. Cool, ugh? Each spawn area has it’s own coordinates pool from which a random X and Y coordinates will be chosen. So when a random zone index is calculated I check it’s value. If it’s zero then X coordinate and Y coordinate is calculated randomly from those specific ranges. In this case spawn position will be somewhere about here in this area. If that index is one then X and Y coordinates will be calculated the way a spawn position will be somewhere within this area. And so on for the third and forth spawn zones. Index equals to two. Spawn position will be here. Index equals to 3. Spawn position will be here. Pretty simple. When coordinates are calculated I give those to spawn position. After that a new enemy is instantiated at that position. And when an enemy is spawned it gets a random color tint. That’s the script.
Let’s hit play and see how it works. I must say it works pretty well. A new randomly coloured enemy is spawned every two seconds. Once spawned, the enemy starts moving towards the player. Hope you like this tutorial. Thank you for watching and see you next time.
#AmongUs #UnitySpawn #UnityRandomSpawn
Episode 6. Spawn Enemies At Random Position With Random Color In Among Us 2D Shooter Clone Made With Unity
I play games here - twitch.tv/zzotoff
You are very welcome :-)
Previous Video
Episode 5. Make Enemy Follow The Player In 2D Top Down Game Like Among Us Shooter Clone Made With Unity youtu.be/jdAoLlCn2e8
Hi!
In this video I show you a simple script that allows you to spawn enemies at random position with random color. Once spawned an enemy starts moving towards the player. This feature was created in a previous video. Link is in the description. So here is my project from that previous video. Player is blue. Enemy is pink. Nothing is new here except this script named Enemy spawner. This script will be attached to Enemy spawner game object which I’m creating right away. There we go. Here it is right in the middle. I take this script and D&D it into the enemy spawner game object. Once script is attached I have to put a game object to be spawned into this slot. If I want to instantiate a game object then I need a prefab of it. Since I want to spawn enemies I create an enemy prefab by D&Ding enemy game object into prefabs folder. There we go. Prefab is created. Now I can delete this enemy from the hierarchy. And once a prefab is created I can D&D it into enemy spawner script component here. Ok. Now it’s time to take a look how this script works. Here it is.
Variables, variables, variables. And in Start method I begin to invoke Spawn New Enemy method every two seconds. What’s inside this method? First of all I randomly choose at which area a new enemy will be spawned. Let me show you. There are four zones or areas where I will be spawning enemies. One area is about here. Second one is about here. Third one i about here. And forth one is about here. So a new enemy will be spawned outside the play zone and will never be spawned inside it. Cool, ugh? Each spawn area has it’s own coordinates pool from which a random X and Y coordinates will be chosen. So when a random zone index is calculated I check it’s value. If it’s zero then X coordinate and Y coordinate is calculated randomly from those specific ranges. In this case spawn position will be somewhere about here in this area. If that index is one then X and Y coordinates will be calculated the way a spawn position will be somewhere within this area. And so on for the third and forth spawn zones. Index equals to two. Spawn position will be here. Index equals to 3. Spawn position will be here. Pretty simple. When coordinates are calculated I give those to spawn position. After that a new enemy is instantiated at that position. And when an enemy is spawned it gets a random color tint. That’s the script.
Let’s hit play and see how it works. I must say it works pretty well. A new randomly coloured enemy is spawned every two seconds. Once spawned, the enemy starts moving towards the player. Hope you like this tutorial. Thank you for watching and see you next time.










