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 /
This question was closed Feb 13, 2019 at 10:31 PM by Elfrick for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Elfrick · Feb 13, 2019 at 06:23 PM · physicscollideroverlapsphere

Problem with the position in OverlapSphere

Hi everyone,

I want to place spheres but to avoid having them overlap, I'm trying to use : Physics.OverlapSphere. I'm using a for loop to generate a new position each time and if it detects a collision, it tries again. My problem is that it seems that the OverlapSphere does not update with the new position. By using a huge radius of exclusion, the problem becomes very visual.

alt text

Here is my code :

 using System.Collections;
 using UnityEngine;
 
 public class Galaxy : MonoBehaviour
 {
     public int nbreStars = 300;
     public int Rmax = 100;
     public float DistBetweenStars;
 
     // Start is called before the first frame update
     void Start()
     {
         int NbreEchec = 0;
 
         for (int i = 0 ; i < nbreStars ; i++)
         {
             float distance = Random.Range (0, Rmax);         
             float angle = Random.Range(0, 2 * Mathf.PI);       
 
             Vector3 StarPosition = new Vector3(distance * Mathf.Cos(angle), 0, distance * Mathf.Sin(angle)); 
 
             Collider[] sphereExclusion = Physics.OverlapSphere(StarPosition, DistBetweenStars); 
                                                                                   
 
             if (sphereExclusion.Length == 0) 
             {
                 GameObject StarGO = GameObject.CreatePrimitive(PrimitiveType.Sphere); 
                 StarGO.transform.position = StarPosition;                
                 NbreEchec = 0;
             }
             else  
             {
                 NbreEchec++;
                 i--;       
             }
 
             if (NbreEchec > nbreStars)                                          
                                         
             {
                 Debug.LogError("Impossible de créer l'étoile, pas assez d'espace ou espace entre les étoiles trop grand !");
                 break;
             }
 
             
         }
     }
 }


The Debug.LogError says "Can't create star, not enough space or space between stars to big" NbreEchec is the number of failing to place the star. NbreStar is the number of stars to place and Rmax is the radius.

So, what do you think I'm doing wrong ? To me, the Physics.OverlapSphere should take a new position for each loop but it seems it doesn't and I don't understand why...

Thank you in advance for your help ! :)

nouvelle-image-bitmap.jpg (89.4 kB)
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

2 Replies

  • Sort: 
avatar image
0

Answer by xxmariofer · Feb 13, 2019 at 07:51 PM

hello, can you test this.

first create 4 floats that will be the max position that the starts will be appearing, since i imagine your game inst infinite

 public float maxWidthPosition;
 public float minWidthPosition;

 public float maxHeightPosition;
 public float minHeightPosition;

and change the code inside the for untill you create the overlapcollider to this

 Vector3 StarPosition = new Vector3(Random.Range(maxWidthPosition, minWidthPosition), 0, Random.Range(maxHeightPosition, minHeightPosition));

 Collider[] sphereExclusion = Physics.OverlapSphere(StarPosition, DistBetweenStars);

Also the way you are checking the if there is enough space for creating the stars is weird too. is there any reason you want to try creating the stars the exact same number of times as the spawn creation failed?

Comment
Add comment · 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
0

Answer by Elfrick · Feb 13, 2019 at 08:08 PM

Thank you for your answer @xxmariofer , It did not fix the issue, with your 4 floats, it created a square space for the stars to be placed.alt text

There is no specific reason for trying as many time to place a star as the number of stars. I just set up an arbitrary number to stop an infinite "for" loop if the code doesn't find space to place a star.

I don't know why the OverlapSphere ignores the "for" loop and doesn't update to the new position :/


capture.jpg (28.4 kB)
Comment
Add comment · Show 6 · 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 xxmariofer · Feb 13, 2019 at 08:12 PM 0
Share

can you share your new code?

avatar image Elfrick xxmariofer · Feb 13, 2019 at 08:21 PM 0
Share

here it is :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Galaxy : $$anonymous$$onoBehaviour
 {
     public int nbreStars = 300;
     public float DistBetweenStars;
     public float maxWidthPosition;
     public float $$anonymous$$WidthPosition;
     public float maxHeightPosition;
     public float $$anonymous$$HeightPosition;
 
     // Start is called before the first frame update
     void Start()
     {
         int NbreEchec = 0;
 
         for (int i = 0 ; i < nbreStars ; i++)
         {
 
             Vector3 StarPosition = new Vector3(Random.Range(maxWidthPosition, $$anonymous$$WidthPosition), 0, Random.Range(maxHeightPosition, $$anonymous$$HeightPosition));
             Collider[] sphereExclusion = Physics.OverlapSphere(StarPosition, DistBetweenStars);
 
             if (sphereExclusion.Length == 0) 
             {
                 GameObject StarGO = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                 StarGO.transform.position = StarPosition;                
                 NbreEchec = 0;
             }
             else  
             {
                 NbreEchec++;
                 i--; 
             }
 
             if (NbreEchec > nbreStars)   
             {
                 Debug.LogError("Impossible de créer l'étoile, pas assez d'espace ou espace entre les étoiles trop grand !");
                 break;
             }
 
             
         }
     }
 }
 
avatar image xxmariofer Elfrick · Feb 13, 2019 at 09:09 PM 1
Share

hello, then the problem is with other part of your code probably i have tested it and worked fine. ill try uploading a gif. but here is the code,

 private void Awake()
 {
     StartCoroutine(enumsss());
 }

 IEnumerator enumsss()
 {

     int counter = 0;
     for (int i = 0; i < 10000; i++)
     {
         Vector3 pos = new Vector3(UnityEngine.Random.Range(maxWidthPosition, $$anonymous$$WidthPosition), 0, UnityEngine.Random.Range(maxHeightPosition, $$anonymous$$HeightPosition));
         Collider[] sphereExclusion = Physics.OverlapSphere(pos, 4);

         if (sphereExclusion.Length == 0)
         {
             GameObject obj = GameObject.CreatePrimitive(PrimitiveType.Sphere);
             obj.transform.position = pos;
             counter = 0;
             
         }
         else { counter++;

             if(counter > 500)
             {
                 Debug.Log("HAVE TESTED 50 TI$$anonymous$$ES WITHOUT LUC$$anonymous$$");
                 yield break;
             }
          }

         yield return new WaitForEndOfFrame();
     }
 }

Show more comments

Follow this Question

Answers Answers and Comments

252 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Very confused... Physics.OverlapSphere working, but not NonAlloc version? 2 Answers

Physics.OverlapSphere not detecting collision! Collision help. 1 Answer

Find what size sphere collider can fit in a point 1 Answer

Configerable joint problem !! 0 Answers

How to do a Placement object properly? 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