Hi there,
I’m currently working on a topdown 2D game and have a question about spawning stuff like Weeds or rocks after a certain amount of time.
What I’m trying to achieve:
-
Similar to games like Stardew Valley and Forager, I am trying to make a spawner that spawns weeds, rocks, or even trees that the player can interact with (chop them or gather them etc).
-
This would need a timer, and something that checks if the area is empty.
Where I am lost:
So right now I have my tilemap, which is the background. I then want to spawn trees, weeds, and other objects at random which I can cut or collect.
I am just unsure if I need to make a separate grid for this, that checks if the cell is empty, and if it is, it can spawn an item at random. Or, if somehow I should just do this without a grid?
I’m also a little confused as to how I achieve this the best and what the best practice is for this.
Below is a video, with a timestamp. At the timestamp, you see a bunch of ores that randomly spawned. I’m trying to create something similar. As far as I can tell, the spawner checks if the area is empty (on a grid?) and then it spawns say 1 object on a random cell that is empty every 1 minute or so.
Anyone got any idea on how to do this?
I did this but I don’t think it’s right…
public int numberToSpawn;
public List<GameObject> spawnPool;
public void SpawnObjects()
{
for (int i = 0; i < 10; i++)
{
float spawnY = Random.Range
(Camera.main.ScreenToWorldPoint(new Vector2(0, 0)).y, Camera.main.ScreenToWorldPoint(new Vector2(0, Screen.height)).y);
float spawnX = Random.Range
(Camera.main.ScreenToWorldPoint(new Vector2(0, 0)).x, Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, 0)).x);
Vector2 spawnPosition = new Vector2(spawnX, spawnY);
Instantiate(spawnPool, spawnPosition, Quaternion.identity);
}
}