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 Mar 19, 2020 at 07:15 PM by IMinoI for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by IMinoI · Mar 18, 2020 at 11:52 AM · positionvariable

OverlapSphere won't accept a variable as position (stays instead at 0,0,0?)

Hi everyone,

i have the same problem as @Elfrick here: https://answers.unity.com/questions/1601488/problem-with-the-position-in-overlapsphere.html

The fix he was told uses cartesian coordinates, but i like to retain the orbital arrangement and want to know what I did wrong.

I tried many things, but i cant get the Collider to accept the variable, it seems to use (0,0,0) instead of the new coordinates. It should be impossible to have a sphere in the middle of the Collider, too, but there it is. The code should check the space around each sphere and prevent other spheres to be created near another sphere.alt text My code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Galaxy : MonoBehaviour
 {
     // TODO: Have these values import from user settings
     public int numberOfStars = 300;
     public int maximumRadius = 100;
 
     public float minDistBetweenStars;
 
     // Start is called before the first frame update
     void Start()
     {
         int failCount = 0;
 
         for (int i = 0; i < numberOfStars; i++)
         {
 
             float distance = Random.Range(0, maximumRadius);
             float angle = Random.Range(0, 2 * Mathf.PI);
 
             var startPosition = new Vector3(distance * Mathf.Cos(angle), 0, distance * Mathf.Sin(angle));
             // Vector3 startPosition = new Vector3(distance * Mathf.Cos(angle), 0, distance * Mathf.Sin(angle));
             Collider[] positionCollider = Physics.OverlapSphere(startPosition, minDistBetweenStars);
 
             if (positionCollider.Length == 0)
             {
                 GameObject.CreatePrimitive(PrimitiveType.Sphere).transform.position = startPosition;
                 failCount = 0;
             }
             else
             {
                 i--;
                 failCount++;
             }
             if (failCount > numberOfStars)
             {
                 Debug.LogError("Could not fit all the stars in the galaxy. Distance between stars is too big!");
                 break;
             }
         }
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 }
 

After changing the code to the one in the comments, i get this picture: alt text

collider.jpg (27.3 kB)
overlapsphereb.jpg (70.8 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

1 Reply

  • Sort: 
avatar image
0

Answer by IMinoI · Mar 19, 2020 at 11:14 AM

@xxmariofer, can you reupload the gif? The code you posted has the same problem, as there will be a circular void around the origin, not around othere spheres.

Interestingly, if you set the position of the OverlapSphere at the origin (0,0,0), other than the first object, you can't generate other spheres, even if you set the Radius of OverlapSphere to 0. I believe it has something to do with GameObject.CreatePrimitive(PrimitiveType.Sphere), which will be created at the origin, too. But i can't seem to find away to create it elsewhere, just transform.position it to somewhere else after the creation at the origin.

The situation with Overlapsphere does not make any sense at all. Even setting a static position like (10,0,10) as the position of the OverlapSphere with Vector3 will generate a void around the origin.

Comment
Add comment · Show 5 · 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 · Mar 19, 2020 at 11:30 AM 1
Share

i didnt have the gif anymore so i have just created an empty project, create an script and pasted the code, worked without issues, maxwidth $$anonymous$$width... were max 100 and $$anonymous$$ 0

https://drive.google.com/open?id=1oVTcD9-qpeomj3aFuFPVnW5ZZkUocB7b

USE THE CODE I POSTED IN THE CO$$anonymous$$$$anonymous$$ENT NOT THE ONE HE POSTED WITH $$anonymous$$Y FIXES

avatar image IMinoI xxmariofer · Mar 19, 2020 at 01:09 PM 0
Share

Thanks for the answer @xxmariofer ! I tried your script you posted in your comment, but raised the radius of the OverlapSphere to 60 and get the same problem as before. I posted a picture of it in the main post up above (overlapsphereb.jpg). The code is: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class Galaxy : $$anonymous$$onoBehaviour {
 
     void Start(){
 
         int counter = 0;
 
         for (int i = 0; i < 10000; i++)
         {   
             Vector3 pos = new Vector3(UnityEngine.Random.Range(100, 0), 0, UnityEngine.Random.Range(100, 0));
             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("Test");
                     yield break;
                 }
             }
         }
     }
     // void Update()
     // {
         
     // }
 }
 
 
 
 
avatar image xxmariofer IMinoI · Mar 19, 2020 at 03:24 PM 0
Share

But thats not the code I posted, check the last comments. Copy the exact code and little by little start doing changes but start with the coroutine (ienumerstor)

Show more comments

Follow this Question

Answers Answers and Comments

231 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

Related Questions

How to make sure an object is at a certain position when a float has a certain value? 0 Answers

Vector2.MoveTowards Not Working 1 Answer

Switch Position of two moving objects 1 Answer

Need help with this code? thanks. 2 Answers

Weird position change 1 Answer


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