- Home /
OverlapSphere for parallel arrays
How can i perform OverlapSphere for parallel arrays which detect when there is a overlapping of prefab gameObjects ? Here is my code which generates these prefabs in random positions and sizes.
void Platform_Position_Scale_Generator(int i) {
posX[i] = Random.Range(minPosRange, maxPosRange + 1);
posY[i] = Random.Range(minPosRange, maxPosRange + 1);
posZ[i] = 0;
scaleX[i] = Random.Range(minScaleRange, maxScaleRange + 1);
scaleY[i] = 1;
scaleZ[i] = 1;
}
void Platform_Generator(int i) {
platformPrefabPosition[i].x = posX[i];
platformPrefabPosition[i].y = posY[i];
platformPrefabPosition[i].z = posZ[i];
Instantiate(platformPrefab, platformPrefabPosition[i], Quaternion.identity);
platformPrefab.transform.localScale = new Vector3(scaleX[i], 1, 1);
}
What exactly are you asking? Do you want to check if the platforms you've just instantiated are overlapping any of the other platforms?
If the platforms are box shaped, it'll be really easy to check if they're intersecting each other by checking if their bounds are intersecting.
Yes they are box shaped colliders. The problem is i don't know how to check if there are overlapping. I need help in that
About how many are you making? That'll influence the answer quite a bit.
How does it ? For now i am making ten platforms. But maybe later if it works. It might vary to how much i will instantiate.
Answer by Baste · Nov 06, 2014 at 03:52 PM
Okay, so using an OverlapSphere to solve your problem is probably not going to work. This is simply because the OverlapSphere is a sphere, while your platforms are (probably rectangular) boxes. So if you have two long platforms that's just over each other, a sphere will detect them as "collided", even though they're not.
So, as long as you don't have very many platforms, the easiest thing will be to check each new platform against every other, to see if their bounds overlap. This will get very slow very fast if you're trying to do it with many platforms, though.
So something like:
//field variable, remember 'using System.Collections.Generic;'
List<Collider> platformColliders = new List<Collider>();
void Platform_Generator(int i) {
platformPrefabPosition[i].x = posX[i];
platformPrefabPosition[i].y = posY[i];
platformPrefabPosition[i].z = posZ[i];
GameObject newPlatform = Instantiate(platformPrefab, platformPrefabPosition[i], Quaternion.identity) as GameObject;
//fixed a bug here, don't change the scale of the prefab, change the instantiated object instead
newPlatform.transform.localScale = new Vector3(scaleX[i], 1, 1);
Collider newPlatformCollider = newPlatform.collider;
bool overlaps = false;
foreach (Collider c in platformColliders) {
if (c.bounds.Intersects(newPlatformCollider.bounds)) {
overlaps = true;
}
}
if (overlaps) {
//You've detected overlapping! Congratulations!
}
else {
platformColliders.Add(newPlatformCollider);
}
}
So if you want to remove overlapping platforms, you just do
Destroy(newPlatform);
under if(overlaps). Now, as I said, this will be fine with 10-20 or even maybe more platforms, but since you're checking the bounds of every platform against the bounds of every other platform, for 100 platforms that'll get you in the vicinity of 10.000 checks, and you'll probably need to look for something else.
Oh thanks man. Thats exactly what i wanted. Im just wondering, how do i code it so when overlaps is true it instantiates another one. Why can't i just do i--
Your answer
Follow this Question
Related Questions
Prefabs instantiated from an array are keeping their public int value 1 Answer
Instantiating an array of objects - how can I instantiate a certain prefab only once? 1 Answer
How to deal with for loop and array? 0 Answers
How to compare and detect overlapping gameObject Prefabs using maths 0 Answers
Instantiate prefab once help C# 1 Answer