- Home /
Stick Sphere Object to another Sphere Object : 2D
I am developing demo project in that i want to stick sphere below another sphere..
Sample game is Here,..
Actually I am learning unity and do not have knowledge about all concepts so i am not getting way to implement this kind of arrangement of sphere , when i shoot new sphere. how do I get point between two sphere on collision.?
Till now i have generated sphere dinamically at perticular position and shooting functionality also done. Now i get stuck at this point..
Please suggest me some idea. Thanks In Advance For Your Support And Help..
Answer by robertbu · Mar 15, 2013 at 02:32 PM
If you watch the video, they are not sticking the bubbles to another bubble. They have a set of points which represent the potential positions of the bubbles. When a bubble hits another of a different color, they are sending it to the closest, empty position. By doing it this way, they maintain the order of the bubbles. If this is the functionality you want, I can describe a way to get it.
But to answer your question about getting the position between two bubbles, you could just use Collision.contats to find where it hit, or you can use the position of the two bubbles when they hit and calculate the position:
function OnCollision(collision : Collision)
{
var pointBetween = (collision.transform.position - transform.position);
pointBetween = transform.position + pointBetween * (0.5 - gap);
}
Gap is the fraction amount away from the center (so the bubbles are spaced). If gap == 0, then this code will find the spot half way between the two bubbles. There are other ways to calculate this position, but as I mentioned above, it looks like they are sending the bubble to the nearest empty place on the board.
yes sir , i want to sent it to the nearest empty position. Can you please explain me the way of doing it??
The layout for all the levels I saw was offsetting likes like you would find in a hex grid. If I was going to do a lot of levels, I work up an editor, but to get started you can create your levels in Unity. Place all the bubbles in their positions, then mark all the empty positions adjacent to the bubbles by placing a game object at these positions. To make it easy, use something with a mesh. You can turn the renderer after you have done the layout. Something like this:
$$anonymous$$ake sure all the game objects representing empty, adjacent positions have the same tag. Then in start you can build a list. Something like:
List emptyList = new List();
void Start () {
GameObject[] argoEmpty = GameObject.FindGameObjectsWithTag("Empty");
foreach (GameObject go in argoEmpty)
emptyList.Add (go.transform);
}
Now you have a list of all the empty positions. Any time you delete a bubble, you would add the transform of that position to emptyList. Then when you want to find the position, just compare the position of "ball" with all the transforms in the list to find the closest position. Here is an untested method to search the list of transforms:
Transform ClosestTransform(Transform curr) {
float fDist = $$anonymous$$athf.Infinity;
Vector3 v3Curr = curr.position;
Transform trOut = emptyList[0];
foreach (Transform trans in emptyList) {
float fT = (trans.position - v3Curr).sqr$$anonymous$$agnitude;
if (fT < fDist) {
trOut = trans;
fDist = fT;
}
}
return trOut;
}