- Home /
Need help on fast and efficient logic of destroying random balls.
Hi, I am new bee, and I want to build 2D game in Unity 3d. Game is just like when user ball hit any other ball and destroying those who are near to collider ball like I am showing in screen shot three blue ball I want to delete from array and also destroy. I know how to destroy and delete from array but getting stuck in logic building. Let say If hit most left blue ball which has 4th index in array and then I'll compare this 4th Index object with rest of array's object if it is less or equal to 0.2 then destroy and also delete, so if 4th index distance equal to 5th index and same 5th index equal to 6th index and then compare to rest of, I think it'll have to check n for n. Kindly suggest me fast and efficient logic for this. I have attached screen shot and also code which is incomplete.
void OnCollisionEnter2D(Collision2D coll) {
GameObject[] temp = new GameObject[10];
int tempCounter=0;
for(int x =0;x<CreateDynamicsObjects.blueBallsArray.Length;x++){
if(CreateDynamicsObjects.blueBallsArray[x].name==name){
temp[tempCounter]=CreateDynamicsObjects.blueBallsArray[x];
tempCounter++;
break;
}
}
int loopCounter =0;
while(temp.Length>loopCounter){
for(int y =0;y<CreateDynamicsObjects.blueBallsArray.Length;y++){
float distance = Vector3.Distance(temp[loopCounter].transform.position,CreateDynamicsObjects.blueBallsArray[y].transform.position);
if(distance<0.3){
for(int z=0;z<tempCounter;z++){
if(CreateDynamicsObjects.blueBallsArray[y].name!=temp[z].name){
if(temp.Length>tempCounter){
temp[tempCounter]=CreateDynamicsObjects.blueBallsArray[y];
tempCounter++;
}
}
}
}
}
loopCounter++;
}
if(temp.Length>0){
for(int i=0;i<temp.Length;i++){
Destroy(temp[i]);
}
} }
CreateDynamicsObjects.blueBallsArray is my array for blue ball. Thanks.
With the way your code is written I don't think it's possible for anyone to read it. Could you please follow at least some standard of syntax and possibly add a comment or two?
@Benproductions1 - It's 2014, outdenting is the new indenting... ;)
@tampshimi I'm fine if people outdent code, as long as it stays readable. But when people put close brackets on new lines, then later in the code they don't and then they indent this, outdent that, leave a space there and don't over here...
Also:
It's 2014, code backwards we write:
{
;"lolz"(printf)
;0(return)
} main void
What have I done...
Why dont you use collider with ontriggerEnter() function.
@Benproductions1 Thanks, and sorry for bad Identation. It was just tried but not complete. Could you please do this via sudo code for this task, if Possible for you. Thanks again.