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
3
Question by DougMAIR · Feb 17, 2015 at 09:15 PM · collisionobjectlist

How do I get List of all objects touching during a collision

During a collision, is there a way to get a list of all objects that are touching the object just collided with?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by sumeetkhobare · Feb 17, 2015 at 10:15 PM

Try looking into Physics.OverlapSphere It checks for colliders around your gameobject in a specific radius. There are other 'Overlap*' functions like circle, box, etc. But it won't take in consideration your gameobject's collider(which may be complex like enveloping your whole mesh).

Comment
Add comment · Show 2 · 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
avatar image sumeetkhobare · Feb 17, 2015 at 10:25 PM -1
Share

$$anonymous$$ark the question as solved if it helps.

avatar image DougMAIR · Feb 18, 2015 at 06:14 AM 0
Share

Thanks, this is what I was looking for.

avatar image
10

Answer by reinfeldx · Feb 18, 2015 at 03:26 AM

Try this. Let me know if you have any questions.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;    // Don't forget to add this if using a List.
 
 public class CollisionList : MonoBehaviour {
 
     // Declare and initialize a new List of GameObjects called currentCollisions.
     List <GameObject> currentCollisions = new List <GameObject> ();
     
     void OnCollisionEnter (Collision col) {
 
         // Add the GameObject collided with to the list.
         currentCollisions.Add (col.gameObject);
 
         // Print the entire list to the console.
         foreach (GameObject gObject in currentCollisions) {
             print (gObject.name);
         }
     }
 
     void OnCollisionExit (Collision col) {
 
         // Remove the GameObject collided with from the list.
         currentCollisions.Remove (col.gameObject);
 
         // Print the entire list to the console.
         foreach (GameObject gObject in currentCollisions) {
             print (gObject.name);
         }
     }
 }
Comment
Add comment · Show 4 · 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
avatar image bartm4n · Feb 18, 2015 at 04:49 AM 1
Share

So basically what I said? lol

"Give the man a fish and he will eat for a day, but $$anonymous$$ch him to fish and he will eat for a lifetime."

avatar image reinfeldx · Feb 18, 2015 at 05:11 AM 1
Share

I've found that reviewing others' commented code is the best way to $$anonymous$$ch myself how to fish ;)

Anyway, I'm new round these parts and this was my first stab at an answer. Faux pas? $$anonymous$$ods, feel free to merge if necessary.

avatar image DougMAIR · Feb 18, 2015 at 06:13 AM 0
Share

This is useful, but I've heard that OnCollisionExit is not always reliable. That's why I didn't accept this answer.

avatar image jesrmaniux · May 19, 2017 at 03:26 AM 0
Share

hi, im currentli using the code you pist, but how can i update it?, im making a rubiks cube, and it only works for me fot the fisrt move, i nead to update the list so i can move the diferent objects.

avatar image
1

Answer by bartm4n · Feb 18, 2015 at 01:56 AM

I don't believe that there is a built in way to do this, but alas it is still possible. I'm not going to write the code for you, but I will give you some tips.

I'm not 100% sure how you are planning to use this information so that might slightly change how you would implement this.

With that said, you should look into OnCollisionEnter and OnCollisionExit. You would also maintain a list in the same script to track active collisions.

During OnCollisionEnter you would add the collided-with GameObject (derived from the Collision info) to the list and during OnCollisionExit you would remove the GameObject from the list.

I hope that this helps. Good luck!

Comment
Add comment · Show 1 · 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
avatar image HypothicEntropy · May 26, 2017 at 03:05 AM 0
Share

What do you mean by:

"You would also maintain a list in the same script to track active collisions."

How do you do this? How do you get a list of objects "currently" colliding with the collider? For instance, Enter/Exit doesn't always work because sometimes Exit is never called on (for instance, when the object collider is disabled or changes layers.

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

6 People are following this question.

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

Related Questions

A node in a childnode? 1 Answer

on collision, show/hide other model scripting ? 1 Answer

Checking Object Collision Without Script 1 Answer

object pool - how to use it / best practice 1 Answer

Can an object to transfer to another as it increases your speed? 1 Answer


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