0
\$\begingroup\$

I'm working on a Tile-Based game and I can't figure out how to make my tiles fall down smoothly like in candy crush. All column tiles fall-down at the same time. And spawn new like how many tile place if null.

What I want:

ExampleSameColumnDown

Update [25.12.2015]

But the my code works like this:

MyCode

Update [26.12.2015]

Hello Again,

World Scale (x,y) World Scale OLD: (10,10) World Scale NEW: (10,11)

Y=11 Tiles type = Empty

Now, when i click the random place like (5,7),above all blocks down to : block.transform.pozition += new Vector3(0,-1,0); It's working. But again, when i click same place (5,7) not working...

I changed Y scale 10 to 11. Because, when i clicked tile, the tile will return to Y=11 position and fall down, how many tile place null.

When Tiles state "fallling down", the space between two blocks distance not more "0.01".

I do not know how to do. But I approached. I need to more improve this.

Thanks...

Pseudo-code:

When I click the (4,7) coord, the tiles,
tile(4,8) is smooth down the coord(4,7)
tile(4,9) is smooth down the coord(4,8)
tile(4,9) is generate new tile.

What I have (Code not working properly :( ):

World.Height = 10
World.Widht = 10
Tile X and Y {get, protected set}


public void ColumnDown(int x , int yStart)
{
    if (IsColumnFull(x)) return;

    for (int y = yStart; y < World.Height; y++)
    {
        //Yeni tile spawnla
        if (y + 1 >= World.Height) continue;

        Tile tile = World.GetTileAt(x, y);
        Tile tile2 = World.GetTileAt(x, y + 1);

        GameObject go_Tile = GameObject.Find("Tile-(" + x + "," + y + ")");
        GameObject go_Tile2 = GameObject.Find("Tile-(" + x + "," + (y + 1) + ")");

        if (tile != null)
        {
            if (go_Tile == null && go_Tile2 == null)
            {
                Debug.Log("go_Tile NULL");
                continue;
            }

            tile2 = tile;
            go_Tile2.transform.position += new Vector3(0, -1, 0);
            go_Tile.name = "Tile-(" + x + "," + (y + 1) + ")";
            go_Tile2.name = "Tile-(" + x + "," + (y) + ")";           
        }
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ set the translate animation \$\endgroup\$ Dec 25, 2015 at 12:10
  • \$\begingroup\$ translate animation is useless. I dont use unity's gravity, collider, animation etc. only world coordinates, gameobject and camera. \$\endgroup\$
    – Dentrax
    Dec 25, 2015 at 12:13

2 Answers 2

3
\$\begingroup\$

Instead of treating the entire column as one entity, have each candy check if it can fall down.

Each candy checks if it collides with a candy below it (or the floor). If not the candy falls down -using gravity so they fall nice and realistic. Since the candy can only fall down per column, collisions are easy to check. Do a count for each vertical and have a new candy spawn on top if the number of candy is lower than the total candy a column can contain.

Keep track if any candy has fallen during the update loop. If no candy has fallen the board is 'locked' and a grid is created from the current candy to do your 'match 3' logic.

The beauty of each candy being a separate entity is that you can do collision detection that is beyond the grid. If you ever played the Disney Frozen match-3 game, you'll notice that a horizontal beam can also hit objects that are falling. So you only need to perform the match-3 logic when the board is in a 'locked' state.

Advanced logic can be created to have certain parts of the board to be locked so gameplay can be more fluid (the player can swap tiles while other area's of the board are still falling).

\$\endgroup\$
8
  • \$\begingroup\$ I'm not using unity's gravity, collions etc... Only using local positions. The FallDown bool is a good idea. My English is not very good.I do not very well understand what you're saying. Can you help me with sample codes or algortihms ? Thanks... \$\endgroup\$
    – Dentrax
    Dec 24, 2015 at 12:43
  • 2
    \$\begingroup\$ You don't have to use unity physics to simulate gravity. Gravity is a simple formula that makes the y velocity faster each loop. If you want to simulate candy falling down smooth you'll have to model each candy individually. \$\endgroup\$
    – Felsir
    Dec 24, 2015 at 12:51
  • \$\begingroup\$ Have you sample source or algorithm for this? Or can you fix a bit my code? (: \$\endgroup\$
    – Dentrax
    Dec 24, 2015 at 14:27
  • \$\begingroup\$ For gravity do something like this: g=9.8f (some constant that works for your game). Each update: acceleration+=g*timestep; candy.Y+=acceleration*timestep; \$\endgroup\$
    – Felsir
    Dec 24, 2015 at 14:54
  • \$\begingroup\$ Thanks, and for my other question, my code not working properly have some issuies o dont know how to fix it... \$\endgroup\$
    – Dentrax
    Dec 24, 2015 at 15:41
-1
\$\begingroup\$

The solution is simple: just use the built-in Physic Engine.

Here's how:

  • Instead of using "Box Collider 2D", use "Circle Collider 2D", this will solve the block-stuck problem.

  • You just need the fall physic, so lock X and Z in RigidBody2D's Constraints. Don't worry, you can still control X via your script.

enter image description here

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .