Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 mfvlk · Mar 21, 2020 at 12:45 PM · collision detectionspawning problemsoverlapsphere

[Solved] Detecting objects with OverlapSphere within the frame they are created?

Hey,

I’ve been trying to procedurally generate several "road segments“ with obstacles at random positions on top of the segments. This has to be done when the scene loads. So my approach was, to solve this with a while-loop inside the Start() method and check for overlaps with Physics.OverlapSphere. I ran into the problem, that Physics.OverlapSphere returns either no object at all or every object on a specific segment when setting the radius very high. After several different attempts in solving this issue, I thought that maybe this has to do with the fact, that everything happens within one singular frame. I already found this post to a similar problem: https://answers.unity.com/questions/587465/collision-detection-within-1-frame.html

Does this also apply to Physics.OverlapSphere? If so, are there ways to solve this differently?

I hope, someone can help me with this!

Thanks

My Code: (Simplified, because the functionality is split across several scripts)

    void Start()
    {
     while (attempts < maxObjects) 
      {
       PlaceObstacleOnRoad(obstacle, segment);
       attempts++;
       }
    }
    void PlaceObstacleOnRoad(GameObject obstacle, GameObject segment)
         {
             Collider segmentcol = segment.GetComponent<Collider>();
             Collider obstaclecol = obstacle.GetComponent<Collider>();
         
             //Get Range of Possible Positions on Segment
         
             float placementZMax =  0.5f - (obstaclecol.bounds.extents.z / segment.transform.localScale.z); 
            //Local Position Max

            float placementZMin = -0.5f + (obstaclecol.bounds.extents.z / segment.transform.localScale.z); 
            //Local Position Min
         
             float placementZPosition = Random.Range(placementZMin, placementZMax);
             Vector3 placementPosition = new Vector3(obstaclecol.bounds.size.x, 0f, placementZPosition);
         
             Collider[] overlaps;
         
       overlaps = Physics.OverlapSphere(segment.transform.TransformPoint(placementPosition), radius);
                     
             if (overlaps.Length < 1)
                 { 
                    //Spawn Code Here
                 }
 }


[1]: https://answers.unity.com/questions/587465/collision-detection-within-1-frame.html

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 mfvlk · Mar 22, 2020 at 04:54 PM

I solved it by turning the function wrapping the while-loop into a coroutine and using WaitForFixedUpdate().

Comment
Add comment · Show 4 · 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 Don_Raphael · Apr 30, 2020 at 11:38 AM 0
Share

I think I may be having a similar problem is there any chance you can expand on your own answer? Not sure how you would structure the coroutine (where you call the WaitForFixedUpdate() or where you call the coroutine generally?)w

Thanks!

avatar image mfvlk Don_Raphael · Apr 30, 2020 at 12:14 PM 0
Share

Yes, sure! Basicly you'd just have to call WaitForFixedUpdate() inside the while-loop, you use to spawn the "obstacles" with. I have put it at the end, right after I spawned the new instance, so that I could be sure, if the while-loop is executed again, all the colliders are already updated by unity, if that makes sense. Would look like this:

  do
             {
                
                //Check if obstacle can be spawned
     
                  //Spawn it
     
                 //Wait for FixedUpdate to update Colliders
                 yield return new WaitForFixedUpdate();
     
             } while (rounds < maxRounds);

$$anonymous$$y coroutine only consists of that (do-)while-loop. So you can call it anywhere you would want that loop to happen. Hope that helps :)

avatar image Don_Raphael mfvlk · Apr 30, 2020 at 01:00 PM 1
Share

Literally just came back to say that I worked it out! Thanks so much this had sent me into a rage yesterday feel like an absolute muppet now hah!

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

130 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

Related Questions

physics.overlapshere not working as expected with objects being placed at random 2 Answers

How to check if 2 collisions are on the same objects ? (is player on the same ground than enemy?) 2 Answers

Preventing Spawn Overlap With Spheres 1 Answer

Detect collider collision ,identify and access colliding colliders 0 Answers

Why does this not work (Involves instantiating objects in response to a collision*) 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