Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by aleem.razzaq09 · Jan 15, 2014 at 06:43 AM · collision

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. alt text

screen shot 2014-01-15 at 11.40.54 am.png (48.1 kB)
Comment
Add comment · Show 10
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Benproductions1 · Jan 15, 2014 at 09:31 AM 0
Share

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?

avatar image tanoshimi · Jan 15, 2014 at 09:41 AM 0
Share

@Benproductions1 - It's 2014, outdenting is the new indenting... ;)

avatar image Benproductions1 · Jan 15, 2014 at 09:55 AM 0
Share

@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...

avatar image ankush_Kushwaha · Jan 15, 2014 at 10:11 AM 0
Share

Why dont you use collider with ontriggerEnter() function.

avatar image aleem.razzaq09 · Jan 15, 2014 at 12:30 PM 0
Share

@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.

Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Fast moving object passing through other objects. 6 Answers

Custom Controller 1 Answer

Collision detection for sprite 1 Answer

Moving Platforms and Collision 1 Answer

2D:I have a movementController script on my character but it doesn't move forward 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges