Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by hamdanb234 · Dec 31, 2020 at 10:38 AM · 2d gamescript.

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 :))

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
0
Best Answer

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

Comment
Add comment · Show 10 · 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 hamdanb234 · Jan 01, 2021 at 08:54 AM 0
Share

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 :)

avatar image Llama_w_2Ls hamdanb234 · Jan 01, 2021 at 11:15 AM 0
Share

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

@hamdanb234

avatar image Unity_Cybertech Llama_w_2Ls · Jun 15, 2021 at 06:36 AM 0
Share

I would like to know what to put in the chunk bit please

avatar image hamdanb234 · Jan 01, 2021 at 01:59 PM 1
Share

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!.

avatar image Unity_Cybertech hamdanb234 · Jun 15, 2021 at 02:03 AM 0
Share

How do you use the Current Chunk Position. What do you put in there, I'm having trouble with it.

avatar image Unity_Cybertech · Jun 15, 2021 at 02:04 AM 0
Share

What do you put in the current chunk position please @Llama_w_2Ls

avatar image Llama_w_2Ls Unity_Cybertech · Jun 15, 2021 at 08:57 AM 0
Share

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

avatar image Unity_Cybertech Llama_w_2Ls · Jun 16, 2021 at 05:49 AM 0
Share

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

Show more comments

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

169 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

Related Questions

Can't add script behaviour while compiling ,Can't add script behavour while compling 1 Answer

Hiding system,loot system and enemy field of view 0 Answers

Trying to make a side scroller. I'm following a tutorial and getting this error. 1 Answer

creating a Temporary powerup 1 Answer

Communication from Native android app to Unity 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