- Home /
comparing transform positions with those from a vector3 array
I'm creating a bubble shooter and although i have most parts working i'm stuck at the part where a ball should be aligned to a grid, and tracking wich balls are attached to the other. I got to the part to create a grid and saving positions to an array, but i can't seem to figure out how to compare it to the values of the array, and i still have no idea how i'm going to implement the tracking of the attached balls..
Here's the script wich creates the grid, the balls have seperate scripts.
var positionPrefab : Transform;
;
function Start ()
{
for(var u : int = 0; u < 17; u++)
{
for(var v : int = 0; v < 15; v++)
{
Instantiate(positionPrefab, Vector3((u * 10) -75, (v * 10) - 70, 0), Quaternion.identity);
}
}
var gridPositions = GameObject.FindGameObjectsWithTag("gridPosition");
var positionLength = gridPositions.length;
var positions :Vector3[] = new Vector3[positionLength];
for(var i = 0; i < positionLength; i++)
{
positions[i] = gridPositions[i].transform.position;
//player[0].transform.position.x;
print(positions[i]);
}
}
function Update ()
{
}
Answer by robertbu · Jun 09, 2013 at 03:16 PM
The bubble games I'm aware of layout the bubbles so that every-other line is shifted. The result is that if you take any one bubble from the middle of the layout it will have six neighbors, all equal distant. You can think of the bubbles as being position on a hex grid. Given this layout, it is easy to determine neighbors. You can run through an array of position and look for any bubbles less than a threshold value. Or you can just use Physics.OverlapSphere() to find the neighbors. For deleting, you would recursively detect bubbles of the same color.
It looks like to me you implemented a rectangular grid. I don't think this works well for a bubble game since neighbors are unclear both to the player and to your code.
I know there is at least one other "thread of questions" about bubble shooting. Here is one answer:
http://answers.unity3d.com/questions/417477/stick-sphere-object-to-another-sphere-object-2d.html