I have 10 blocks lined up in a row. When the user clicks a block it moves to a new Vector3 position. What I want is when the user clicks lets say a block in the middle, i want all of the blocks to the right of the clicked block to shift one block to the left. I have the block positions stored in a list. What currently is happening is that when I click a block in the middle it moves as it should, but the other blocks don’t shift to the left. Then when I click the block BEFORE the 1st clicked block, then the rest of the blocks shift over 1. I know my logic is wrong somewhere here slightly.
public void UpdateBlockPositions(int blockNumberMoved)
{
for (int q = 0; q < TotalPlayerLetters; q++)
{
Block b = (Block)Blocks[q].GetComponent(typeof(Block));
if (b.WordBoardPosition > blockNumberMoved)
{
Blocks[q].transform.position = BlockPositions[b.WordBoardPosition - 1];
b.WordBoardPosition -= 1;
}
}
Each block maintains a public variable WordBoardPosition that stores the current position number of each block.
Instead, why don(t you make a second array ?
ArrayPosition (your current array)
0 - Position 1
1 - Position 2
…
n - Position n+1
ArrayCurrentBlock
0 - Block 1
1 - Block 2
2 - Block 3
3 - Block 4
And for each element in ArrayCurrentBlock (ACB), you retrieve the position of equal index in ArrayPosition (AP).
So :
AP [0] → ACB [0]
AP [1] → ACB [1]
etc.
Even if you remove an element in ArrayCurrentBlock, it will always follow the pattern above.
Heres another attempt trying to reflect what you suggested. This time each block moves to the right instead of the left. I’m sure I just have some simple logic problem.
for (int p = 0; p < BlockTags.Count; p++)
{
for (int x = 0; x < TotalPlayerLetters; x++)
{
if (BlockTags[p] == Blocks[x].tag)
{
Block b = (Block)Blocks[x].GetComponent(typeof(Block));
b.WordBoardPosition -= 1;
Blocks[x].transform.position = BlockPositions[p];
}
}
}
ok. so i’ve tested all of the logic. It is working just fine. My problem is in transforming the game object in this loop. For some reason it will not move things as they need to go. I know the logic is working because I did a test where when you click a block, rather than the blocks in front of it shifting to the left, they just change in scale. It worked beautifully. In fact, I even hard coded the amount to move and it still wont listen:
Blocks[q].transform.position = new Vector3(Blocks[q].transform.position.x - 68f,Blocks[q].transform.position.y,Blocks[q].transform.position.z);
So there is some problem with my transform of the gameobjects. Any ideas?
problem solved.
The touch event was occurring multiple times (GetButton) and hence messing up the transform position.
So yeah…lesson of the day is make sure you check the other factors in the design besides the logic.