Help with the spawning

Hi, I did his Conde but the problem is that it spawn the obstacles all in the same moment… so when it is game over it doesn’t stop spawning.

Any idea? Thanks :slight_smile:

{
public GameObject player;
public GameObject obstacle;
Vector2 pos;
[SerializeField] float spawRadius;
public bool stopSpawn = false;
public int i = 0;
public static VeroSpawner current;

void Awake()
{
if (current == null)
current = this;
}
// Start is called before the first frame update
void Start()
{
pos = player.transform.position;
}

// Update is called once per frame
void Update()
{
while (!stopSpawn)
{
SpawOstacoli();
i++;
if (i == 10)
stopSpawn = true;
}
}

void SpawOstacoli()
{
spawRadius = Random.Range(8, 20);
Vector2 spawnPoint = GetPointOnUnitCircleCircumference() * spawRadius;
Instantiate(ostacolo, spawnPoint, Quaternion.identity );
}

public Vector2 GetPointOnUnitCircleCircumference()
{
float randomAngle = Random.Range(0f, Mathf.PI * 2f);
return new Vector2(((Mathf.Sin(randomAngle))), (Mathf.Cos(randomAngle))).normalized;
}
}

Please use code tags .

I don’t understand your explanation for what you are trying to do or how the current result is wrong.

i want to spawn randomly on a circumference enemies the problem is that with this code the enemies are spawned all at the same time. I would like to spawn one by one

The problem lies in this part I think.

// Update is called once per frame
void Update()
{
while (!stopSpawn)
{
SpawOstacoli();
i++;
if (i == 10)
stopSpawn = true;
}

Like it says on top, this is done every frame. so, if your pc is doing 100fps it will look like instant spawn.

Instead of i++ try something like i = i/Time.DeltaTime/2

Then u divide i by the time the frame took to build and then divide that by the amount of seconds you want between spawns.

I hope I got that right, I’m pretty new at this scripting thing.

… Oh, you probably have to make i a float instead of an int. And change the ‘if (i == 10)’ into something like ‘if (i >= 10)’.

1 Like

I have the feeling you also need an if statement for the ‘SpawOstacoli();’

if (i < 10)
{
SpawOstacoli();
}

Because when i hits 10 you also want the spawning to stop i guess :face_with_spiral_eyes:.

Let me know if something I wrote helped.

1 Like