I'm making a 2D top down game where the player runs around and random segments will spawn so that they are constantly surrounded by a 3x3 group of segments giving the illusion of an endless map. These segments have a box collider and a huge amount of primitive colliders as child objects. (100-300 colliders per segment give or take) and a bunch of sprites. Here is an example of a segment:
![alt text][1]
[1]: /storage/temp/33487-segmentexample.png
I'm using an object pooling system with a total of 24 segments, 9 being used at a time and the rest off screen (disabling them caused a bigger spike than just moving them). No matter what method I try I can't escape the massive physics.simulate spike. I've considered trying a mesh collider instead of primitives but that would require an enormous amount of work with all the segments I've already made and may not even help. Does anyone know a way to remove or reduce the spike caused by this? Note that I've tried having a kinematic ridgedbody on all colliders, just the parent colliders, and on none of the colliders and it doesn't seem to make much of a difference.
Here's the code that 'generates' the segments:
IEnumerator GenSegments() {
//Move all segments outside of the 3x3 box back into the segment pool (objects at y -1000 are in this pool)
foreach (Transform OtherSeg in UseableSegments)
{
if (Vector3.Distance(CurrentSegment.position, OtherSeg.position) > 80 && OtherSeg.position.y > -500)
{
//Move the segment
OtherSeg.position = new Vector3(0, -1000, 0);
}
//Stagnate the spike to avoid a frameskip
yield return new WaitForFixedUpdate();
}
//CheckPos is used to check for existing segments before moving in a new one
Vector3 CheckPos = new Vector3 (CurrentSegment.position.x + 50, 25, CurrentSegment.position.z);
//Checks all 9 spots where a segment can be
for (int A = 0; A < 8; A++)
{
if (!Physics.Raycast (CheckPos, Vector3.down, out SegmentRay, 30f, SegmentLayerMask))
{
//If there is no segment, grab a random one from the pool and move it to the empty spot
Transform CurrentSeg = null;
int RandomInt = Random.Range(0, 11);
while (!CurrentSeg)
{
if (UseableSegments[RandomInt].position.y < -500) CurrentSeg = UseableSegments[RandomInt];
else {
RandomInt++;
if (RandomInt > 11) RandomInt = 0;
}
}
//Move the segment
CurrentSeg.position = new Vector3(CheckPos.x, 0, CheckPos.z);
}
//Used to move CheckPos to the next segment spot
if (A == 1 || A == 2){
CheckPos.x -= 50;
} else if (A == 3 || A == 4){
CheckPos.z -= 50;
} else if (A == 5 || A == 6){
CheckPos.x += 50;
} else if (A == 0){
CheckPos.z += 50;
}
//Stagnate the spike to avoid a frameskip
yield return new WaitForFixedUpdate();
}
}
I am using 3D colliders as I found that most of the 2D features did not have top down games in mind and that made things difficult. All movement is based on raycasting so there is no actual physics. I would greatly appreciate any help you gives can give.
↧