I'm trying to take an audio button, when it is clicked turn it into 8 bars that I can animate. I don't understand how to get the spacing properly. I understand a cube has a pivot in the middle of its mesh. I tried basing things between -.5 and .5, -1 and 1 etc and couldn't get it working. I then tried to base the numbers off transform.localScale.x + y and couldn't get that working. I guess I just don't fundamentally understand how I'm supposed to approach this. None of what i consider logical is working how I expect
private void GenerateAudioVisualization()
{
float margin = 0.02f;
int cubeCount = 8;
// Work in normalized space (container is -0.5 to 0.5)
float totalWidth = 1f;
float totalHeight = 1f;
float totalMargins = margin * (cubeCount - 1);
float cubeWidth = (totalWidth - totalMargins) / cubeCount;
float startX = -0.5f + (cubeWidth / 2f);
for (int i = 0; i < cubeCount; i++)
{
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.SetParent(audioVisualizationContainer.transform);
cube.transform.localScale = new Vector3(cubeWidth, totalHeight, cubeWidth);
float xPos = startX + i * (cubeWidth + margin);
cube.transform.localPosition = new Vector3(xPos, 0, 0);
cube.name = $"AudioCube_{i}";
}
}