- Home /
Transforming position for different colliders for repeating background?
In my main camera, I have a BoxCollider2D (looper) as a child. It is set up far to the left of the camera. The camera is following my player. I have 6 background panels and 7 ground panels. These grounds and background have a trigger boxcollider on them so whenever the 'looper' collides with the trigger background, that background would go up front of the last background, repeating it. However, my ground is shorter than my background so i'm having a bit of trouble. The background transforms position well but the ground seems to overlap each other. I know there's something missing. I'm totally new to unity, about a week old now. I have thought of a solution wherein I would make my ground panels as long as the background panels so they would change position at the same time but I still want to figure this one out since I want to learn. Check my script.
int bgPanels = 6; // Number of our background.
void OnTriggerEnter2D(Collider2D collider) {
float sizeOfBGObject = ((BoxCollider2D)collider).size.x; // The size of a background is equal to the size of the collider.
Vector3 pos = collider.transform.position;
pos.x += sizeOfBGObject * bgPanels; // The size of the background multiplied by the number of how many backgrounds are there, this way it would go next of the last background.
collider.transform.position = pos; // We change the position of the collider to the result of 'pos'
}
I know what's going on. The ground is moving too, but since it's shorter and there's seven of them, the size of the ground is just being multiplied by 6 instead of 7 that's why there are overlaps. I just want to know if somehow I can tell the collider "If you collided with the background, the position would be equal to the size of it multiplied by 6 but if you collided with the ground, the position would be equal to the size of it multiplied by 7". Thank you so much! :)