- Home /
How does "Balls?" (by Dani) Spawning system work?
Does anyone know how to implement Spawning system just like Dani's game "Balls?", i've tried everything thing that came in my mind but nothing work as it should. Any help from some genius peoples will be appreciated. THANKYOU! (Waiting to see some genius ideas :))
Answer by Llama_w_2Ls · Dec 31, 2020 at 11:28 AM
I watched his devlog thingy like a while ago and I forgot what it looked like, but after a quick google images search, I think I have an idea of how it actually works.
So apart from the actual player movement, the spawning of prefabs occur when the player enters an area where he hasn't entered, like how chunks work in minecraft, and then spawns a random collection of 'balls' in the player's radius.
I'm guessing the map is split up like a grid, and 'chunks' are created when the player has entered an area outside the chunk's radius that they are in. Then, the position of the balls might be generated using Random.insideUnitSphere
multiple times (which generates a random point inside of a sphere with a radius of 1) as well as a small check to make sure that the balls don't spawn inside of each other, or too close. This occurs every time a new chunk is loaded, im guessing. @hamdanb234
so do i need to make empty gameobjects and place them all around my map to see if player has entered in one of them?.... Some line of codes would be helpful :)
Not necessarily. Remember that we are instantiating the balls, and therefore we only need to generate a random spawn point and instantiate. We don't need to pre-populate the area with gameobjects or spawnpoints.
You would have to look more into chunks to see the best way to spawn the balls and reduce the performance struggles of spawning a huge map at once, instead of only spawning in areas where the player hasn't reached yet. The code for this might look as follows:
public class Spawner : $$anonymous$$onoBehaviour
{
public GameObject Player;
public GameObject BallPrefab;
public List<Vector2> ChunkPositions;
public float Range = 10f;
public int NumberOfBalls = 5;
Vector2 CurrentChunkPos;
private void Update()
{
CurrentChunkPos = // Closest chunk the player is nearest to
if (Vector2.Distance(Player.transform.position, CurrentChunkPos) >= 10)
{
// Create new chunk and add the position to the chunk positions
CreateChunk();
}
}
private void CreateChunk()
{
// Get a position and add it to the list
// Randomly generate spawn points
for (int i = 0; i < NumberOfBalls; i++)
{
Vector3 RandomPosOffset = Random.insideUnitSphere * Range;
Vector2 SpawnPoint = new Vector2(RandomPosOffset.x + CurrentChunkPos.x, RandomPosOffset.y + CurrentChunkPos.y);
Instantiate(BallPrefab, SpawnPoint, Quaternion.identity);
}
}
}
I would like to know what to put in the chunk bit please
Now I understand, I just have to learn more about chunks then i'll be ready to go. Thankyou @Llama_w_2Ls for this good explanation!.
How do you use the Current Chunk Position. What do you put in there, I'm having trouble with it.
What do you put in the current chunk position please @Llama_w_2Ls
The current chunk position is the closest Vector2 in the List<Vector2> ChunkPositions
to the player. You can calculate this by sorting the list by distance to the player and getting the first element. For example:
// Sorted by closest positions to player
// (make sure to have 'using System.Linq' at the top of your script)
var sortedPositions = ChunkPositions.OrderBy(x => Vector2.Distance(x, Player.position));
// The closest chunk pos
CurrentChunkPos = sortedPositions[0];
At the beginning of the script, there will be no chunk positions, so just add the player's current position as the first chunk position, and grow from there.
Chunks are a relatively complicated concept. It's hard to explain how to do it properly, and I'm not the person to talk to about the efficiency of it either. Hope you figure it out though.. @Unity_Cybertech
Hi, its co$$anonymous$$g up with an error that the array isn't co$$anonymous$$g through. I had to change the position of player to transform.position otherwise it wouldn't work. using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Linq; public class SpawnerScript : MonoBehaviour { public GameObject Player; public GameObject BallPrefab; public List<Vector2> ChunkPositions; public float Range = 10f; public int NumberOfBalls = 5; Vector2 CurrentChunkPos; public void Update() { var sortedPositions = ChunkPositions.OrderBy(x => Vector2.Distance(x, Player.transform.position)); CurrentChunkPos = sortedPositions[0]; // Closest chunk the player is nearest to if (Vector2.Distance(Player.transform.position, CurrentChunkPos) >= 10) { // Create new chunk and add the position to the chunk positions CreateChunk(); } } private void CreateChunk() { // Get a position and add it to the list // Randomly generate spawn points for (int i = 0; i < NumberOfBalls; i++) { Vector3 RandomPosOffset = Random.insideUnitSphere * Range; Vector2 SpawnPoint = new Vector2(RandomPosOffset.x + CurrentChunkPos.x, RandomPosOffset.y + CurrentChunkPos.y); Instantiate(BallPrefab, SpawnPoint, Quaternion.identity); } } }