- Home /
How do I dynamically scale and move groups of objects without them overlapping?
I am making a 2D endless runner and I have coins that are created at random sizes and recycled as the player progresses through the world. I apply a random scale value to the coin sprite and then add another directly next to it.
My code works well when I use it on a single prefab coin but I want to expand its functionality to work with a prefab that uses an empty gameObject with coins as children so I could create various patterns of coins for the player to pick up.
The problem that I am running into is that the prefabs overlap each other when I apply different scales to each and try to put them next to each other dynamically. I think the issue is with how I am trying to find the size of the objects using encapsulate, but I'm not sure.
Could someone please take a look at my recycle code and let me know if they can see anything obvious that I am doing wrong?
private void Recycle(){
Transform temp = objectQueue.Dequeue ();
float adjustedDistance = 0.0f;
oldBounds = totalBounds;
if (useRandomGapSize) {
gapSize = Random.Range (minGap, maxGap);
}
if (useRandomHeight) {
float height = 0;
height = Random.Range (minHeight, maxHeight);
nextPosition.y = height;
}
if (useRandomScale) {
scale = new Vector2 (Random.Range (minScale.x, maxScale.x), Random.Range(minScale.y, maxScale.y));
}
temp.localScale = scale;
if (isGroup) {
Transform[] groupOfObjects;
int childCount = temp.childCount;
groupOfObjects = new Transform[childCount];
totalBounds = new Bounds(temp.position, Vector3.zero);
for (int i = 0; i < childCount; i++){
groupOfObjects[i] = temp.GetChild(i);
totalBounds.Encapsulate(groupOfObjects[i].renderer.bounds);
}
adjustedDistance = totalBounds.size.x;
} else {
sprite = temp.GetComponent<SpriteRenderer> ();
spriteSize = sprite.bounds.size;
Vector2 oldSpriteSize = spriteSize;
spriteSize.x = sprite.bounds.size.x * temp.localScale.x;
spriteSize.y = sprite.bounds.size.y * temp.localScale.y;
adjustedDistance = (spriteSize.x + oldSpriteSize.x) * 0.5f;
}
nextPosition.x += adjustedDistance + gapSize;
temp.localPosition = nextPosition;
objectQueue.Enqueue (temp);
}
Answer by Owen-Reynolds · Feb 18, 2014 at 04:34 PM
I'm not seeing the purpose of all that code, but ...
Seems, with coins all children of an empty parent, you could just scale the parent. All sizes and distances (object centers) should scale perfectly together.
Only issue would be with rotated non-uniform scaling problem (x*2, y*1, z*3.) Might need to always scale all axii the same.
The parent is being scaled but, I thought that in order to know where to place the next set of coins, I would need to figure out the bounds of all of the coins inside of the empty group. Are you saying that I don't need to do that?
If you know the original bounds, of the "coin set," scaling the parent should linearly scale the bounds. Say you have a sphere trigger. It should always scale to perfectly fit, as the parent does (but spheres really, really, want x,y&z all scaled the same.)
I mean, sure, gathering and combining bounds by hand is fun, and a shame to let Unity have the pleasure of doing it automatically, but there's plenty of stuff to program that Unity won't do for you :-)
Your answer