#Generating cubes that fill in a container GameObject

1 messages · Page 1 of 1 (latest)

void yoke
#

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}";
        }
    }
#

Here are all views of the container and game objects

frail beacon
#

Seems like an issue with the startX calculation. You want it to be nagative: (-(total_width_of_everything / 2)+ half_cube_width), so that it's centered on the button.

wind thistle
#

Looks like the AudioCube and AudioCubeVisualizationContainer objects have different scales applied, the audio cube is (0.64, 0.45, 0.3).
If you set them to have the same scale it should look better…

void yoke
#

@frail beacon + @wind thistle : Thank you for your help. So I created another object inside AudioVisualizationContainer called AudioVisualizationInsert (with exact same scale as the AudioCube object) and now am inserting the bars into that. Which seems closer to what I was looking for.

#

So if the problem is now that my calculations are off...I don't understand how I am suppose to get the variables total_width_of_everything and half_cube_width). I haven't change my code at all yet, the image is showing the code posted above still.

void yoke
#

Better photo @

#

I just don't understand the scale..it seems like its nonsense to me...I know its not...I'm sure it makes perfect sense for some scenerios but I can't figure out how to make my approach work.

wind thistle
#

You need to check the scale of all the parent game objects… children are all multiplied with each parent.
So if your objects are using different parents, which they are based on your current hierarchy, then you need to make sure that all scale for their parents matches… otherwise they will render incorrectly.
Based on the screenshot it still looks like there is still a scale variance between the AudioCube and the ones you have created

void yoke
#

Ok, I think I have a basic understanding how scale works and how it is impacted by all the scale settings of parents. I don't understand how mine are wrong but lets ignore that for now. Lets say my scale is perfect. What is the method I am suppose to use to calculate the size of the bars.

wind thistle
#

Did you fix the AudioCube position? Just noticed your previous screens had it with a -0.324 offset.

But, for the width calculation, it mostly looks fine.
There is the assumption what the width is 1… You could get the bounds from the renderer of the AudioCube and use that rather than hard coding it. But if you are sure it is 1 then should be fine.
The startX looks like it should be: -0.5f + (cubeWidth + margin)/2f (it is missing the margin at the moment)
Not sure about the totalMargins… I think that should just be cubeCount, not -1. And you are adding that margin for the last cube (in the loop), so the widths aren’t quite right. I would just use cubeCount rather than (cubeCount-1)

void yoke
#

The audioCube is the orange one...it is in the perfect place and it is my reference that I'm trying to duplicate

wind thistle
#

You need to replicate its position and scale to the Visualization container so that they match… plus check parents

void yoke
#

I tried to get the size from the renderer prior and it gave errors:

Renderer renderer = audioVisualizationContainer.GetComponent<Renderer>();
float containerWidth = renderer.bounds.size.x;
float containerHeight = renderer.bounds.size.y;

There is no 'Renderer' attached to the "AudioVisualizationContainer" game object, but a script is trying to access it.

#

I can't do anything without knowing the size I'm trying to replicate and I can't figure that out

wind thistle
#

from the AudioCube… that is the one that you are trying to use as thing to match against isn’t it…

#

If it is just a reference that you are going to delete later then don’t worry… but that makes it really important that the positions and transforms of everything in the hierarchy are the same between the two branches