• 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
1
Question by EpsilonQoppa · Jun 24, 2017 at 11:36 PM · collider.oncollisionenter

How to avoid duplicate collisions on object with same behaviour?

So, I'm using OnCollisionEnter to detect collision between objects and when they collide, they produce a particle effect. Like so:

 void OnCollisionEnter(Collision collision)
 {
      EmitEffectAtVector(collision.contacts[0].point);
 }

However, due to the nature of OnCollisionEnter, it's creating two instances of the particle systems on each collision which is unnecessary at best.

Anybody have any ideas on how to avoid this? Having multiple behaviors for the same prefab just isn't reasonable.

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

1 Reply

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

Answer by Hellium · Jun 25, 2017 at 09:28 AM

A simpler solution, compare the instance IDs of the scripts :

 void OnCollisionEnter(Collision collision)
  {
       ScriptName script = collision.collider.GetComponent<ScriptName>();

       // Makes sure only the script with the lowest instance ID will call the function
       if( script != null && script.GetInstanceID() > GetInstanceID() )
       {
             EmitEffectAtVector(collision.contacts[0].point);
       }
  }
Comment
Add comment · Show 5 · 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 EpsilonQoppa · Jun 26, 2017 at 09:26 PM 0
Share

Sorry for the delay. The issue I have with this is that I'm using object pools and the objects instance ID is not always new and/or greater than. Honestly, that's just an assumption as the API docs don't say much..

avatar image Hellium EpsilonQoppa · Jun 26, 2017 at 09:36 PM 0
Share

It does not matter at all. Relying on the instance ID is simply the way to make sure only one of the two objects colliding will call your function.

avatar image EpsilonQoppa Hellium · Jun 26, 2017 at 09:58 PM 0
Share

Oh, I get it. Either way one of them is going to be greater, than, huh? That's slick. Thank you, sire.

Show more comments
avatar image phaselis_ · Mar 30 at 01:56 AM 0
Share

It works for 2 objects but i can't stop them duplicating when there are more than 2 objects. Is there any other way to avoid multiple collisions?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

70 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to find the velocity of a point in a collider? 1 Answer

PolygonCollider2D edit problem 1 Answer

How do I know who attacked who? 3 Answers

Carcassonne style mechanic using box colliders to check edge type for a match,script for checking if two boxCollisions share a variable type? Carcassonne! 0 Answers

Checking collisions between two objects that don't have rigidbodies 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