- Home /
How to prevent multiple spawns from a single spawn point?
Hey everyone,
I'm working on an endless runner style game where the player has to dodge blocks while collecting coins. I'm using four spawn points to spawn three blocks and one coin using the code below:
void SpawnBlocks()
{
int randomIndex = Random.Range(0, spawnPoints.Length);
for (int i = 0; i < spawnPoints.Length; i++)
{
if (randomIndex != i)
{
Instantiate(blockPrefab, spawnPoints[i].position, Quaternion.identity);
Instantiate(coinPrefab, spawnPoints[randomIndex].position, Quaternion.identity);
}
}
}
Initially, I started the project by randomly spawning three blocks among four spawn points, so there would always be a gap for the player to go through. To make the game more engaging, I decided to spawn a collectable coin within the gap using whichever spawn point is not being used to spawn a block.
According to this script, i = all of the spawn points that are being activated in each instance. I had to figure out which spawn point was not being activated so that I could use that spawn point to spawn a coin. After simply realizing that the randomIndex != i, I figured out that I could use spawnPoints[randomIndex].position to spawn the coin because the randomIndex number is the number that is never being used for the block spawn points.
This worked flawlessly, or so I thought. When I tried implementing the score system, which would +1 for every coin that the player collected, I noticed that each time the player collected a coin, the score would +3 instead. That's when I realized that, instead of spawning just one coin, I was spawning three coins on top of each other.
I would like to prevent the spawn point from spawning three coins at once and, instead, just spawn one coin, but I can't wrap my head around how to do that with my current script.
I've also seen people utilize a check function to see if an object is already in the location before spawning another, which would work if these coins weren't spawning at the exact same time.
My last idea is to change the score value from +1 per coin to +0.3 per coin, and then round the 0.9 from the stacked coins into a 1. This might be my best option, but I can't quite figure out how to add the decimals together and then round to a whole number (I'm basically one DNA molecule away from a chimp, after all).
Apologies for the reading assignment, I thought it'd be best to fully explain my situation to receive proper assistance. If anybody knows of a solution/workaround then I would greatly appreciate it!
Answer by losingisfun · Mar 17, 2021 at 03:17 AM
What you could do is create a pool and then remove the last used spawn point. Here's an example:
public void SpawnBlocks() {
// Create a pool
List<Vector3> pool = new List<Vector3>();
foreach (Vector3 v in spawnPoints) { pool.Add(v); }
for (int i = 0; i < spawnPoints.Length-1; i++) {
int randomIndex = Random.Range(0, pool.Count);
Instantiate(blockPrefab, pool[randomIndex], Quaternion.identity);
pool.RemoveAt(randomIndex);
}
// Now add the coin with the last remaining spawn point in the pool
Instantiate(coinPrefab, pool[0], Quaternion.identity);
}
Really fascinating method! Its implementation is above my pay grade but that just provides the motivation to read into it more. I'll check in with you if I can get this to work. I appreciate you sharing your thought process!
Your answer
Follow this Question
Related Questions
How to spawn all humans at once and activate then with time? 1 Answer
Random spawnpoint where only one prefab can spawn! 1 Answer
How to spawn enemies at different locations and avoid overlapping each other 2 Answers
How do I spawn more the one spawnpoints ramdomly in my scene / Spawning problems 0 Answers