- Home /
[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
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()
.
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!
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 :)
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!
Your answer
Follow this Question
Related Questions
physics.overlapshere not working as expected with objects being placed at random 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
how to access parent script from child object with overlapsphere in a multiplayer game 0 Answers