- Home /
Generating blocks with bad distance of each other.
Hello. I have some problem with generating my platforms in 2D platformer game. I am generating and disabling theese blocks with object pooling method. I have pooler for every size of block. For example pooler 7x1, pooler 6x5... and theese poolers are handled as array in my generator here:
using UnityEngine;
public class PlatformGenerator : MonoBehaviour {
private float[] platform_widths;
private float[] platform_heights;
private int platform_selector;
private float[] platform_widths_total;
public Transform generation_point;
public ObjectPooler[] poolers;
void Start () {
platform_widths = new float[poolers.Length];
platform_heights = new float[poolers.Length];
for(int i = 0; i < poolers.Length; i++) {
platform_widths[i] = poolers[i].pooled_object.GetComponent<BoxCollider2D>().size.x;
platform_heights[i] = poolers[i].pooled_object.GetComponent<BoxCollider2D>().size.y;
}
}
void Update () {
platform_selector = Random.Range(0, poolers.Length);
if (transform.position.x < generation_point.position.x) {
transform.position = new Vector3(
transform.position.x + platform_widths[platform_selector] - 1,
transform.position.y + platform_heights[platform_selector] - 1,
transform.position.z
);
GameObject new_platform = poolers[platform_selector].GedPooledObject();
new_platform.transform.position = transform.position;
new_platform.transform.rotation = transform.rotation;
new_platform.SetActive(true);
}
}
}
Problem is, that theese blocks are not fitting in each other. When i had one type of blocks with length of 6 (6x1, 6x2, 6x3, ... 6x6) everything was fine. They were generating without gaps. But now they are generating with them and I dont know why. When i assign to transform.position new vector, i need to subtract they length of one block becouse of that fitting to each other one down and one left. But with new sizes of blocks they are positioned +2 to right or -2 to left depends of their size of box collider. (His size and offset is setted fine.) Do someone has any idea why this is doing this?
Your answer
![](https://koobas.hobune.stream/wayback/20220612174121im_/https://answers.unity.com/themes/thub/images/avi.jpg)