My code works right without LeanTween but it works incorrectly with Tween. However, the variables are the same

It is a really weird problem.

This is my working code:

if (tileToMove != null && tileToMove.gameObject != null)
{
    tileToMove.YPosition = targetRow;
    Vector3 newPosition = tileToMove.transform.position;
    float distance = GameManager.Instance.tileEdge;
    newPosition.y = newPosition.y - distance;
    tileToMove.transform.position = newPosition;
}

It basically makes the cells in grid fall:
bandicam 2024-11-16 03-41-50-765

But obviously it looks ugly and I wanna use animations. So I updated it with LeanTween:

if (tileToMove != null && tileToMove.gameObject != null)
{
    tileToMove.YPosition = targetRow;
    Vector3 newPosition = tileToMove.transform.position;
    float distance = GameManager.Instance.tileEdge;
    newPosition.y = newPosition.y - distance;
    // Use LeanTween to animate the movement
    LeanTween.moveY(tileToMove.gameObject, newPosition.y, 0.3f) // 0.3 seconds for the animation
        .setEase(LeanTweenType.easeInOutCubic); // Smooth animation easing
}

bandicam 2024-11-16 03-45-23-196

As you can see, the blocks don’t move to directions they must be. It is really weird since I use the same location in both codes.

What might cause this problem? And ideas?

Note: This is a recursive function. So, the same if loop runs again and again till all conditions are ok.

You got a bug, that’s all. First guess might be you’re not copying the entire array before working on it perhaps?

Whatever it is, that means… time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Make sure you’re storing 2D grid stuff properly too:

Tile-based / grid-based 2D games: match3, tetris, chips challenge, rogue, etc:

For any tile-based game such as Match3 or Tetris or a grid-based Roguelike, do all the logical comparisons in your own data storage mechanism for the tiles, such as a 2D array of tiles.

Otherwise you needlessly bind your game logic into Unity objects and the Unity API, making it about 10x more complicated than it needs to be.

If you have no idea how to work with 2D arrays, hurry to some basic C# tutorials for the language portions of it, then look at any good tutorial that uses a 2D array

Here is my Match3 demo using this technique of storing data in a grid. Full source linked in game comments.

It stores all of its data in a 2D array:

PieceController[,] Board;

This allows for easy simple checking in code, not relying on anything like physics.

You should strive to use that pattern for all logic, then only use Unity to present what is happening in the game logic.

Thank you very much for all your advices. However, the problem is I have a deadline, so I am trying to fix my problems with the shortest way.

I have tried debug with Visual Studio’s debug mode but I have not fixed yet. I guess I should use Debug.Log too.

Yes, I do that actually. Just, I didn’t want to share my whole code. But the interesting thing is my gridMatrix gets updated correctly but the tile positions in the scene are wrong. For example, in gridMatrix[2,2] node, there is a blue cell but in the grid in the emulator there is a red cell in the same location. It only happens when I use LeanTween

Now, I fixed the problem by adding a bool to check if it is animating but now they move separately in animation :frowning:

One moves and the other one waits for it.