• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by yucline · 5 days ago · instantiate2d game

[Beginner question] 2D Time based spawning of objects like weeds or stones/ore

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.

Video is here

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);
         }
     }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Caeser_21 · 5 days ago

I'm also creating a 2d open-world farming game that is somewhat similar to forager...


The way I did i handled spawning of objects : I made my world randomly generated tiles that are simple gameobjects. Then I actually instatintiated the objects as children of the tile... So when I need to check whether the tile is full or not I just check the childCount of the object


BUT, I think you are using a tilemap to make your world.
A possible solution is to this is set up a timer and use raycasts to check whether an object is present in the given position... If you need code for reference on how to do it, I can provide some :)


Reference code :

 [SerializeField] private int MaxX, MaxY, MinX, MinY; //Assign all these in the inspector according to the size of the tilemap
 
 [SerializeField] private GameObject[] SpawnObjs; //These are the objects like weeds, stones, etc...
 
 private float Timer;
 [SerializeField] private float MaxTime; //This is the time between spawns, change accordingly
 
 private void Update()
 {
     Timer += Time.deltaTime;
     if (Timer >= MaxTime)
     {
         int XPos = Random.Range(MinX, MaxX + 1);
         int YPos = Random.Range(MinY, MaxY + 1);
 
         int InstNum = Random.Range(0, SpawnObjs.Length);
 
         Instantiate(SpawnObjs[InstNum], new Vector3(XPos, YPos, 0f), Quaternion.identity);
 
         Timer = 0;
     }
 }

This isn't tested but i think it should work :)


Reference code 2 : Change the code to this if you want to be sure that no objects spawn on top of each other

  [SerializeField] private int MaxX, MaxY, MinX, MinY; //Assign all these in the inspector according to the size of the tilemap
     
     [SerializeField] private GameObject[] SpawnObjs; //These are the objects like weeds, stones, etc...
     
     private float Timer;
     [SerializeField] private float MaxTime; //This is the time between spawns, change accordingly
     
     private void Update()
     {
         Timer += Time.deltaTime;
         if (Timer >= MaxTime)
         {
             int XPos = Random.Range(MinX, MaxX + 1);
             int YPos = Random.Range(MinY, MaxY + 1);
     
             int InstNum = Random.Range(0, SpawnObjs.Length);
             
             RaycastHit2D = hit;
             if (Physics2D.Raycast(new Vector3(XPos, YPos, 1f), Vector3.back, out hit))
             {
                 if (hit.tranform.tag != "InstObj")
                 { 
                    Instantiate(SpawnObjs[InstNum], new Vector3(XPos, YPos, 0f), Quaternion.identity);
                 }
             } 
             Timer = 0;
         }
     }

Important : All the Objects like weeds, stones and whatever you are instantiating must have the tag InstObj


Sorry for not posting this code sooner, i got caught up in some other work... Good Luck :)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image yucline · 5 days ago 0
Share

Hi there, thanks for your comment!

Yes I think I'll stick the method of using a tilemap. In theory my world is static so I can easily design the world, then I'm thinking of adding a grid on it, and spawning the things on the grid.

If you have some reference code that would be greatly appreciated as it would help me understand it a little more.

Thanks again!

avatar image Caeser_21 yucline · 5 days ago 1
Share

I have updated my original answer with some reference code... try it out and see if it works :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

211 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiating randomly sized prefab to the right of another 2D 0 Answers

Instantiate GameObject at Random time is not working 1 Answer

Spawn: Instantiate GameObject 1 Answer

ParticleSystem showing in scene but not in game. 7 Answers

Instantiated Particle System seems to only be playing randomly and isn't being destroyed when it finishes. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges