My Program will ideally create a series of stacked squares based on 2 variables. 1 Is the amount of squares per series and the other is how many series there will be. each series is branched out from the center of the screen at different angles spaced evenly apart. If there are 2 series, one will be facing 0 degrees and the other will be facing 180 degrees. If there is 3, the first will be facing 0 degrees, second will face 120, and third will face 240. Somewhere in my math or my method of making this effect, I messed up bad.

The current result is, for some reason, the Fibonacci spiral. Here is the Are the suspected functions below (with all variable definitions) also, button is a class I made and not a typo:

public button Led; //Object used to create series
public Slider ledNumber;// controls how many LEDS there are per series
public Slider IncrementsPerRotation;//Controls how many series there are
public Text ledText;//text above ledNumber slider
public Text incrementText;//text above IncrementsPerRotation Slider
public Transform canvas;//Canvas where objects will be created
List<List<button>> LedConfig = new List<List<button>>();//where all the LEDs will be stored
private float buttonW, middleX,middleY; // the width of the LED and the middle values of the screen
private int prevLed = 0, prevIncrements = 0;// previous values for both sliders
// Start is called before the first frame update
void Start()
{
    buttonW = Led.transform.localScale.x;// set LED width
    Vector2 temp = Camera.main.ScreenToWorldPoint(new Vector2 (Screen.width / 2, Screen.height / 2));// get middle values

    //assign middle values
    middleX = temp.x;
    middleY = temp.y;
}

// Update is called once per frame
void Update()
{

    if (prevLed != (int)ledNumber.value && (int)ledNumber.value > 0)//has the slider changed and is it positive
    {
        Debug.Log("prevLED Changed");
        if (prevLed < ledNumber.value)// am I removing or adding buttons
        {
            Debug.Log("Adding LED");
            for (int x = 0; x < (int)ledNumber.value - prevLed; ++x)//for the difference in the number changed
            {
                for (int y = 0; y < IncrementsPerRotation.value; y++)//add a led to the top row
                {
                    float angle = 360 / IncrementsPerRotation.value * y;// get angle

                    // if there isnt enough lists, add another one;
                    if(LedConfig.Count-1 < y)
                    {
                        LedConfig.Add(new List<button>());
                        ++prevIncrements;
                    }

                    //object: LED Where: (see GetPos) What angle: angle converted to radians
                    LedConfig[y].Add(Instantiate(Led, GetPos(y, angle), new Quaternion(Quaternion.identity.x, Quaternion.identity.y, angle * (Mathf.PI / 180f), Quaternion.identity.w),canvas));
                    Debug.Log("got past instantiate");
                }
                ++prevLed;
                Debug.Log("end of outer for loop");
            }
        }
        else
        {
            Debug.Log("Removing LED");
            for (int x = 0; x < prevLed - (int)ledNumber.value; ++x)//for the difference in the number changed
            {
                for (int y = 0; y < LedConfig.Count; y++)//delete the top row of LEDs
                {
                    LedConfig[y][LedConfig[y].Count - 1].delete();//delete the LED
                    LedConfig[y].RemoveAt(LedConfig[y].Count - 1);//remove it from the list
                }
                --prevLed;
                Debug.Log("end of outer for loop");
            }
        }
    }
    if (prevIncrements != (int)IncrementsPerRotation.value && (int)IncrementsPerRotation.value > 0)
    {
        deleteAll();
        prevLed = 0;
    }
}
void deleteAll()
{
        for (int x = 0; x < LedConfig.Count; ++x)//for the difference in the number changed
        {
            for (int y = 0; y < LedConfig[x].Count; y++)//delete the top row of LEDs
            {
                LedConfig[y][LedConfig[y].Count - (y + 1)].delete();//delete the LED
                LedConfig[y].RemoveAt(LedConfig[y].Count - (y + 1));//remove it from the list
            }
        }
}

private Vector2 GetPos(int a,float angle)
{
    float angleInRadians = angle * (Mathf.PI / 180f);
    ++a;


    Debug.Log("x " + (buttonW * a) * (Mathf.Sin(angleInRadians) * 180 / Mathf.PI));
    Debug.Log("y " + (buttonW * a) * (Mathf.Cos(angleInRadians) * 180 / Mathf.PI));
    Debug.Log("middleX " + middleX);
    Debug.Log("middleY " + middleY);
    Debug.Log("tot X " + (middleX + buttonW * a * Mathf.Sin(angleInRadians)));
    Debug.Log("tot Y " + (middleY + buttonW * a * Mathf.Cos(angleInRadians)));

    return new Vector2(middleX + buttonW * a * Mathf.Sin(angleInRadians), middleY + buttonW * a * (Mathf.Cos(angleInRadians)));
}

The code is having many errors: Moving the ledNumber creates multiple squares on the same spot

Moving the IncrementsPerRotations causes an index out of range error which pauses the game, if the game is continued (by pressing the pause button) the Fibonacci spiral is produced with the squares and the squares are at random angles.

This is not all the code, only the part which is related to the error.

Edit: Here is a visual aid of what the desired output should look like at increments per rotation values 1-4 and led number at 3

0 Answers