- Home /
SOLVED
Increment position every different value (C# UNITY)
How can I increment the position of the X and Y depending on its value
Here's my code :
#region BIG EYE ROAD
for (int col = 0; col < table.GetLength(0); col++)
{
for (int row = 0; row < table.GetLength(1); row++)
{
if (table[moveCol, moveRow] == null)
{
col0 = table[colIndex1, row];
col1 = table[colIndex2, row];
if (col0 != null)
{
sum1 += 1;
}
if (col1 != null)
{
sum2 += 1;
}
counter1++;
}
else
{
if (counter1 == 0)
{
data1 = table[firstTableCol, firstTableRow];
data2 = table[secondTableCol, secondTableRow];
data3 = table[thirdTableCol, thirdTableRow];
data4 = table[fourthTableCol, fourthTableRow];
}
else
{
if (moveRow > 1)
{
firstTableRow = moveRow;
secondTableRow = moveRow - 1;
thirdTableRow = moveRow;
fourthTableRow = moveRow - 1;
}
else if (moveRow == 1)
{
firstTableRow = 1;
secondTableRow = 0;
thirdTableRow = 1;
fourthTableRow = 0;
}
if (moveCol > 1)
{
firstTableCol = moveCol;
secondTableCol = moveCol;
thirdTableCol = moveCol - 1;
fourthTableCol = moveCol - 1;
}
data1 = table[firstTableCol, firstTableRow];
data2 = table[secondTableCol, secondTableRow];
data3 = table[thirdTableCol, thirdTableRow];
data4 = table[fourthTableCol, fourthTableRow];
}
if (data1 != null && data2 != null && data3 != null && data4 != null)
{
redBead();
}
else if (data1 != null && data2 != null && data3 == null && data4 == null)
{
redBead();
}
else
{
blueBead();
}
if (moveRow + 1 < table.GetLength(1))
{
moveRow++;
}
}
}
if (sum1 == sum2)
{
redBead();
}
else
{
blueBead();
}
//reset
sum1 = 0;
sum2 = 0;
if (colIndex1 + 1 < table.GetLength(0) && colIndex2 + 1 < table.GetLength(0))
{
colIndex1++;
colIndex2++;
}
else
{
//nothing to do
}
if (moveCol + 1 < table.GetLength(0))
{
moveCol += 1;
}
if (moveRow + 1 < table.GetLength(1))
{
moveRow = 1;
}
#endregion
}
}
void blueBead()
{
p = Instantiate(big_eye_gameobject) as GameObject;
p.transform.SetParent(pos_big_eye_road);
p.transform.localScale = Vector3.one;
img = (RawImage)p.GetComponent<RawImage>();
img.texture = NewTexture[0];
p.SetActive(true);
p.transform.localPosition = new Vector3(bigEyeXIndex * 56, bigEyeXIndex * -45, 0f);
}
void redBead()
{
p = Instantiate(big_eye_gameobject) as GameObject;
p.transform.SetParent(pos_big_eye_road);
p.transform.localScale = Vector3.one;
img = (RawImage)p.GetComponent<RawImage>();
img.texture = NewTexture[1];
p.SetActive(true);
p.transform.localPosition = new Vector3(bigEyeXIndex * 56, bigEyeXIndex * -45, 0f);
}
What I am trying to do here: I have this variables called int bigEyeXIndex = 0; and int bigEyeYIndex = 0; . So everytime that my value is different and not the same it will increment my X and Y axis for example like this
But what is happening to me right now is like this
You should improve your description. I have no clue what you are trying to achieve. What is P, B, T? In the second image all letters move to the 1st cell? This happens at once? Or do the grid lines move? Why do you try to calculate positions? Do you try to implement animations or do the letters just jump?
The P , B, T are the values.
All of the letters are in the same spot .
Yes this happen at
I need to calculate the position because everytime when it is a different value for example in the picture .
I'm just trying to implement localPosition everytime the value is different from the last value then it will change position
Wouldn't it be easier to have a grid of cells and only change the parent of the objects to the target cell?
Answer by jim3878 · May 28, 2018 at 03:51 AM
you need to add a new method to calculate next Index and determine that if current color is equals to previous.
enum beadColor{
BLUE,
RED,
NONE,
}
beadColor previous=beadType.NONE;
void blueBead()
{
//your code
OnBeadIncressed(color.BLUE);
p.transform.localPosition = new Vector3(bigEyeXIndex * 56, bigEyeYIndex * -45, 0f);
}
void redBead()
{
//your code
OnBeadIncressed(code.RED);
p.transform.localPosition = new Vector3(bigEyeXIndex * 56, bigEyeYIndex * -45, 0f);
}
void OnBeadIncressed(beadColor color){
if(beadColor==previous){
bigEyeYIndex++;
}else{
bigEyeXIndex++;
}
previous= color;
}
There's a problem with this code sir
it's not incrementing on Y Axis
Oops,I found that at line 13&20
p.transform.localPosition = new Vector3(bigEyeXIndex * 56, bigEyeXIndex * -45, 0f);
must replace to
p.transform.localPosition = new Vector3(bigEyeXIndex * 56, bigEyeYIndex * -45, 0f);