- Home /
Make my grid fall and spawn new objects
Hello.
I'm currently creating a puzzle game where I create a grid, I then want the grid to drop and fill in the spaces of the objects that dissapear and spawn new objects at the top of the playboard (somewhat like candycrush) What is an easy way to achieve this? here you can see what I currently have.
using UnityEngine;
using System.Collections;
public class Grid : MonoBehaviour {
public GameObject Hex;
public GameObject MoleCule;
public int gridWidthInHexes = 10;
public int gridHeightInHexes = 10;
private float hexWidth;
private float hexHeight;
void Start(){
setSizes();
createGrid();
}
void setSizes(){
hexWidth = Hex.renderer.bounds.size.x;
hexHeight = Hex.renderer.bounds.size.y;
}
Vector3 calcInitPos(){
Vector3 initPos;
initPos = new Vector3(-hexWidth * gridWidthInHexes / 2f + hexWidth / 2, 0,
gridHeightInHexes / 2f * hexHeight - hexHeight / 2);
return initPos;
}
public Vector3 calcWorldCoord(Vector2 gridPos){
Vector3 initPos = calcInitPos();
float offset = 0;
if (gridPos.y % 2 != 0)
offset = hexWidth / 2;
float x = initPos.x + offset + gridPos.x * hexWidth;
float y = initPos.y - gridPos.y * hexHeight * 0.29f;
return new Vector3(x, y, 0);
}
void createGrid()
{
GameObject hexGridGO = new GameObject("HexGrid");
for (float y = 0; y < gridHeightInHexes; y++)
{
for (float x = 0; x < gridWidthInHexes; x++)
{
GameObject hex = (GameObject)Instantiate(Hex);
GameObject molecule = (GameObject)Instantiate(MoleCule);
Vector2 gridPos = new Vector2(x, y);
hex.transform.position = calcWorldCoord(gridPos);
hex.transform.parent = hexGridGO.transform;
molecule.transform.position = calcWorldCoord(gridPos);
molecule.transform.parent = hexGridGO.transform;
}
}
}
}
Answer by Quaker_SDR · Sep 01, 2014 at 12:04 PM
Calculate the column count regularly, if the count gt reduced, Add 1 from the top.
How would I execute this? especially with them moving down to the empty spot for example if one in the bottom gets removed every single on on top if it has to move down one and then add a new one from the top, how can I achieve this?
Your answer
Follow this Question
Related Questions
Problem searching grid using recursive function 2 Answers
Block puzzle (1010) help 0 Answers
Puzzle piece rotation 0 Answers
Stacking Lego pieces 2 Answers