Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by DrewCarter101 · Apr 17, 2017 at 03:07 AM · spawnspawning problemsspawner

How to know if there is no possible space on board to spawn.

I have a method that spawns 4 different type of prefabs in order of priority. it spawns the first type, then spawns the next if there is room. There's a number, of objects per type to be spawned, specified. I want it done in a way that if there's no room for an object of lower priority to spawn, then it doesn't spawn. This is the method I have:

 void Spawn( GameObject obj, float height, int num, int max){
     
     currentObjectsOnBoard = 0;//number of specified objects spawned
     for (int i = 0; i < max; i++) {

         if (num == 0) {
             break;
         }

         float x = Random.Range (startX, (startX + boardW));
         float y = height;
         float z = Random.Range (startZ, (startZ - boardH));
         Vector3 temp = new Vector3 (x, y, z);


         Collider[] hitColliders = Physics.OverlapSphere(temp, spacer);
         int a = 0;
         bool free = true;

         while (a < hitColliders.Length)
         {
             if ((hitColliders [a].CompareTag ("Collectable")) || (hitColliders [a].CompareTag ("Obstacle")) || (hitColliders [a].CompareTag ("Hole")) || (hitColliders [a].CompareTag ("Player"))  || (hitColliders [a].CompareTag ("Coin")))  {
                 free=false;
                 //check if spot is occupied by any of the above, within a radius specified by spacer
             }
             a++;
         }

         if (free) {
             Instantiate (obj, temp, Quaternion.identity);
             currentObjectsOnBoard++;
         } else {
             i--;//i did it like this so the maximum number of spawnable items can be fully reached
         }

         if ((currentObjectsOnBoard == num)) {
             break;
             //if object reaches number specified or if no more can be spawned.
         }
             
     }
 }


How I implemented this was before each object is spawned, I calculate the maximum number of that object that can be spawned after objects of higher priority have been spawned...I think my problem is my math. I think I'm not calculating the maximum number spawnable right. I use the are of each object assuming this was a flat 2d board and I multiply that number by the space I want in between all objects. Here's how I implemented the first two:

 maxCollectablesOnBoard = (int) (((boardArea- (playerArea)) / (collectArea)));
     Spawn (collectable, collectAndCoinHeight, collectNum, maxCollectablesOnBoard);

     tempcollect = currentObjectsOnBoard;//number of collectables spawned if different from numer requested
     maxHolesOnBoard = (int) (((boardArea  - (playerArea) - (tempcollect * collectArea)) / (holeArea)));
     Spawn (hole, holeHeight, holeNum, maxHolesOnBoard);


here's an example of how I calculated the area:

 collectArea = (spacer+1) * (Mathf.PI * collectRad * collectRad);

I've been trying this for days and I can't figure it out, I'd really appreciate the help. Thank you!

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

Answer by Cuttlas-U · Apr 17, 2017 at 06:41 AM

hi; i cant really help u with this based on your script and the way u did it;

if it was on me i would do 2 Instantiation first one is just temperory and create an object of the same prefab without meshrenderer but has collider ; this Instantiation has a script on it that works as detector see if it has collision with any thing or not ; if not it will send a request to the main script and send the order to Instantiate the real object and destroy him self cause its notneeded any more;

Comment
Add comment · Show 1 · 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 DrewCarter101 · Apr 20, 2017 at 04:50 PM 0
Share

Hello, I''m sorry I didn't reply, I sort of gave up on the game cause I couldn't figure this out but I will try out your solution and reply if I have any further problems with it. Thank you so much

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

99 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

Related Questions

question about object pooling an array and spawning 0 Answers

problem when i spawn to many Enemy the game be slower 1 Answer

Instantiate isnt working for spawner class 1 Answer

How do I stop multiple spawns? 0 Answers

Can I spawn with an array in order? 2 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