Enemy Spawning Help (2D Platformer, C#)
Hi, I am going to have multiple spawn points with this script attached. And I want the enemies to spawn after a certain amount of time, but only when the player is in a certain range ("maxDistance"). And not spawn enemies at all when the player is not in that range. This part works, the problem is the when I start the game and after the ("spawnDelay"), It spawns enemies rapidly and doesn't stop. It ignores the ("spawnTime"). Please help me with this.
Script (C#)
public class Spawner : MonoBehaviour {
public float spawnTime; // The amount of time between each spawn.
public float spawnDelay; // The amount of time before spawning starts.
public GameObject[] enemies; // Array of enemy prefabs.
public int maxDistance;
public Transform target;
public Transform myTransform;
void Awake()
{
myTransform = transform;
}
void Start()
{
GameObject stop = GameObject.FindGameObjectWithTag("Player");
target = stop.transform;
maxDistance = 7;
}
void FixedUpdate()
{
if (Vector3.Distance(target.position, myTransform.position) < maxDistance)
{
InvokeRepeating("Spawn", spawnDelay, spawnTime);
}
}
void Spawn()
{
// Instantiate a random enemy.
int ZombieIndex = Random.Range(0, enemies.Length);
Instantiate(enemies[ZombieIndex], transform.position, transform.rotation);
}
}
Answer by Ashton-Benedict · May 23, 2016 at 03:59 AM
Found the perfect solution!!! Here is the script if anyone needs it:
public class Spawner : MonoBehaviour {
public float spawnTime; // The amount of time between each spawn.
public float spawnDelay; // The amount of time before spawning starts.
public GameObject enemy;
public int maxDistance;
public Transform target;
public Transform myTransform;
void Awake()
{
myTransform = transform;
}
void Start()
{
GameObject stop = GameObject.FindGameObjectWithTag("Player");
target = stop.transform;
maxDistance = 5;
StartCoroutine(SpawnTimeDelay());
}
IEnumerator SpawnTimeDelay()
{
while (true)
{
if (Vector3.Distance(target.position, myTransform.position) < maxDistance)
{
Instantiate(enemy, transform.position, Quaternion.identity);
yield return new WaitForSeconds(spawnTime);
}
if (Vector3.Distance(target.position, myTransform.position) > maxDistance)
{
yield return null;
}
}
}
}
I had to use a while loop with the WaitForSeconds statement. Thanks for no ones help :P It took me for ever!
Answer by LLIV · May 21, 2016 at 11:29 PM
Instead of using InvokeRepeating just use: Spawn();
It didn't work, its still spawning enemies rapidly. I replaced
InvokeRepeating("Spawn", spawnDelay, spawnTime);
with
Spawn();
Is that what you meant? because it didn't fix it. :/
I tried doing something like this. Spawning the enemy if in range and waiting 7 seconds to spawn again, but it didn't work, maybe something like this ins$$anonymous$$d? Please help.
void FixedUpdate()
{
SpawnTimeDelay();
}
IEnumerator SpawnTimeDelay()
{
if (Vector3.Distance(target.position, myTransform.position) < maxDistance)
{
Spawn();
yield return new WaitForSeconds(7);
}
else
{
yield return null;
}
}
void Spawn()
{
// Instantiate a random enemy.
int ZombieIndex = Random.Range(0, enemies.Length);
Instantiate(enemies[ZombieIndex], transform.position, transform.rotation);
}
}
How do i apply the script in green above because even if i apply it because I've been using this tutorial for a project I'm making and have been looking for an enemy spawner but haven't been the right thing i need
been using this tutorial: https://unity3d.com/learn/tutorials/topics/2d-game-creation/creating-basic-platformer-game
Your answer
![](https://koobas.hobune.stream/wayback/20220612081258im_/https://answers.unity.com/themes/thub/images/avi.jpg)