# Techniques for performance and issue with polar calculations

I am trying to figure out the fundamentals of Unity by making what I thought was a simple project. The idea is that you have a square grid of cubes, and parts of the grid would burst up in sync with music to create an interesting audio visualizer. I am trying to get it working at random points and will do the music part later. Right now, it works perfectly on some settings, but when the circle "radius" gets too big, it doesn't appear to work correctly. The effect that i'm going for is similar to this video (but with a fixed focal point, it should not oscillate but rather go through one cycle): Example video

I also want to make sure since i'm learning Unity that I am being efficient in how I do things. Right now I have a rough idea of C# syntax, so for simplicity I iterate over two (2D?) arrays. There are probably better techniques to do what I am doing, so if you can provide optimizations for a beginner that would be very helpful. I am not sure why the radial ripple effect doesn't work on large values (try 15 gridsize, 3 cubesize, 1.5 growth interval, 25 growth size, and 5 shrink rate to see what I mean). Here is my entire code, which is attached to a standalone object:

```
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CubeGenerator : MonoBehaviour {
public GameObject whiteCube;
public GameObject blackCube;
public int gridSize;
public int cubeSize;
public float growthInterval;
public float growthSize;
public float shrinkRate;
private GameObject[,] cubes;
private bool[,] grownCubes;
private int shrinkScale;
void Start () {
shrinkScale = 3;
//Scale the cubes based on the grid and cube size
whiteCube.transform.localScale = new Vector3(cubeSize, cubeSize, cubeSize);
blackCube.transform.localScale = new Vector3(cubeSize, cubeSize, cubeSize);
cubes = new GameObject[gridSize, gridSize];
grownCubes = new bool[gridSize, gridSize];
for (int x = 0; x < gridSize; x++) {
for (int z = 0; z < gridSize; z++) {
if (x % 2 == z % 2) {
cubes[x, z] = (GameObject)Instantiate(whiteCube, new Vector3 (x * cubeSize, (float)cubeSize / 2, z * cubeSize), Quaternion.identity);
} else {
cubes[x, z] = (GameObject)Instantiate(blackCube, new Vector3 (x * cubeSize, (float)cubeSize / 2, z * cubeSize), Quaternion.identity);
}
grownCubes[x, z] = false;
}
}
}
//Randomly grow a cube at this interval (seconds)
float nextTime = 0;
void Update () {
if (Time.time >= nextTime)
{
int magnitude = (int)growthSize / cubeSize;
Vector3 initialPosition = transform.position;
Vector3 growth = new Vector3(0, growthSize, 0);
int randomX = Random.Range(0, gridSize);
int randomZ = Random.Range(0, gridSize);
//ScaleObject(cubes[randomX, randomZ], growth, 1);
for (int radius = 0; radius < magnitude; radius++)
{
if (radius > 0)
{
int angleDelta = (int)(360 / (4 * radius));
for (int degrees = 0; degrees < 360; degrees += angleDelta)
{
int x = (int)(radius * Mathf.Cos(degToRad(degrees))) + randomX;
int z = (int)(radius * Mathf.Sin(degToRad(degrees))) + randomZ;
if (x >= 0 && x < gridSize && z >= 0 && z < gridSize && grownCubes[x, z] == false)
{
grownCubes[x, z] = true;
ScaleObject(cubes[x, z], growth * Mathf.Pow((float)2 / 3, (float)(radius)), 1);
}
}
}
else
{
ScaleObject(cubes[randomX, randomZ], growth, 1);
}
}
nextTime += growthInterval;
}
//Always shrink cubes if they are over the initial size
foreach (GameObject cube in cubes)
{
if (cube.transform.localScale.y > cubeSize)
{
Vector3 shrink = new Vector3(0, Mathf.Min(shrinkRate, cube.transform.localScale.y - cubeSize) * Time.deltaTime * shrinkScale, 0);
ScaleObject(cube, shrink, -1);
}
}
for (int x = 0; x < gridSize; x++) {
for (int z = 0; z < gridSize; z++) {
grownCubes[x, z] = false;
}
}
}
void ScaleObject (GameObject obj, Vector3 scalar, int direction)
{
obj.transform.localScale += direction * scalar;
obj.transform.position += (direction * scalar) / 2.0f;
}
float degToRad (float degree)
{
return (float)degree * Mathf.PI / 180;
}
}
```

Thank you for bearing with me.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.