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 burnleyboy101 · Aug 15, 2013 at 12:30 PM · collisions

Detecting a string of collisions

I'm making a game, where if 4 balls of a certain colour touch, they all get destroyed. I know how to use OnCollisionEnter() for 2 of the balls touching, or even 3. But if you imagine a line of 4 balls touching

OOOO
1 2 3 4

The first ball and the last ball arn't touching so OnCollisionEnter() won't work. Any advice would be apprechiated. Many thanks,

Burnleyboy101

Comment
Add comment · Show 2
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 ShadoX · Aug 15, 2013 at 12:47 PM 0
Share

Try checking the collisions between the separate balls. I mean check if 1 and 2 are the same color if touching, then check if 2 and 3 are the same and then 3 and 4.

Something that I might suggest is to just have an array that you can go through ins$$anonymous$$d of using OnCollisionEnter() unless the collision is really important. I can imagine that function using a bit more resources than a simple for loop, but I might be wrong on that.

avatar image Sajidfarooq · Sep 03, 2013 at 11:37 PM 0
Share

Are all the balls of the same size? If yes, then you can simply check the bounding box of the "n" balls.

 If (boundingbox.volume <= ball.boundingbox.volume * n) then all of them hitting each other 

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jamora · Sep 04, 2013 at 09:10 AM

You should create a class that describes a collision (say, MyCollisionClass)of multiple objects. Most easily implemented by maintaining a list of GameObject with functions to add and remove objects from the collision.

Now, each object will know which collision they're a part of, so any new colliders can just add themselves to that collision in OnCollisionEnter(). I set the collision class to null when exiting the collision, but a better design decision would be to use the null object pattern.

 public MyCollisionClass{
 /*
 This class contains the list of GameObjects currently 
 engaged in the collision. There should also be a way to
 add/remove GameObjects from the list. I suggest making a
 wrapper for the normal list functions.
 */
 }
 
 public class MyCollider : MonoBehaviour{
     MyCollisionClass currentCollision = null
 
     void OnCollisionEnter(Collision col){
         //you'll need null checks here too
         currentCollision = col.gameObject.GetComponent< MyCollider >().currentCollision
         currentCollision.Add(this.gameObject);
     }
 
     void OnCollisionExit(Collision col){
         currentCollision.Remove(this.gameObject);
         currentCollision = null;
     }
 }

You don't really explain your use case, but this will allow you, for example, to count how many objects are in a collision or gain access to each individual object that makes up that collision. If you need to know each collision, I would make a private static list of MyCollisionClasses inside MyCollisionClass in which each currently active collision is kept.

Comment
Add comment · Share
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

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

18 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

Related Questions

Collisions with CharacterController? 0 Answers

Collision problem Ignorecollision 2 Answers

Sequencing Spawning for Multiplayer 1 Answer

Allow one object to pass through another whilst keeping collisions. 1 Answer

Tree collisions do not work 2 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