1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| using UnityEngine;
public class SpacingObjects : MonoBehaviour
{
public GameObject[] objectsToSpace; // Tableau pour stocker les gameObjects à espacer
public float spacing = 2f; // Espace souhaité entre les gameObjects
void Start()
{
float currentPosition = 0f;
// Placer le premier gameObject à la position initiale
objectsToSpace[0].transform.position = new Vector3(currentPosition, 0f, 0f);
currentPosition += objectsToSpace[0].GetComponent<Renderer>().bounds.size.x;
// Placer les autres gameObjects avec l'espace approprié entre eux
for (int i = 1; i < objectsToSpace.Length; i++)
{
currentPosition += spacing;
objectsToSpace[i].transform.position = new Vector3(currentPosition + objectsToSpace[i].GetComponent<Renderer>().bounds.size.x / 2f, 0f, 0f);
currentPosition += objectsToSpace[i].GetComponent<Renderer>().bounds.size.x / 2f;
}
}
} |
Partager