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 CgShady · Oct 18, 2014 at 09:08 PM · collisionrigidbodycollider

Colliding with similar object

Hi,

I came up to this scenario which I think must be rather common. I have several similar objects (asteroids) floating in space. I want to draw some impact and play sound when they collide with each other.

They are instances of the same Prefab, so they have the same tag and the same Monobehaviour. When they collide, I get two collisions, not just one. One detected by the first, another by the second.

Is there a simple way to just discard the second call ?

Comment
Add comment · Show 3
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 CgShady · Oct 18, 2014 at 09:28 PM 0
Share

For now, what I'm doing is this, but it's far from ideal :

 void OnCollisionEnter2D (Collision2D collision)
     {
         if (collision.gameObject.tag == gameObject.tag)
             collision.gameObject.GetComponent<CollidingObject>().StopCoroutine ("Collide");
 
         StartCoroutine ("Collide", collision);
     }
 
     public IEnumerator Collide (Collision2D collision)
     {
         yield return new WaitForSeconds (0.001f);
         // handle collision
     }
avatar image richyrich · Oct 19, 2014 at 01:13 AM 0
Share

Just an idea, but presumably there is potential for multiple collisions between more than just two objects within a very small interval, so wouldn't you want to prevent those other collisions' sounds too? How about activating a timer (duration of the sound) and allow another play only if the timer had reached zero?

You could count the time from a static float, then the first collision that found the time was up could launch a new play of the sound. Other collisions would see the time was not up and ignore

avatar image CgShady · Oct 19, 2014 at 12:26 PM 0
Share

It makes sense yes, at least for the sound. I'm also concerned about the graphics, as I draw an impact at the point of contact, and when colliding two similar objects, I get two, which isn't so good performance wise, especially that I expect many collisions..

2 Replies

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

Answer by richyrich · Oct 19, 2014 at 01:51 PM

You could compare two GameObjects by their instance IDs, then only choose the ID with the lowest value. Personally, I've being something similar recently, but assigned IDs in a similar manner to the suggestion by DFLY. However, there is another potential issue. You also need to make sure you are not repeating the same collision... you can have more than one collision per frame between the same two colliders. http://docs.unity3d.com/Manual/ExecutionOrder.html

To avoid this issue, you could set a flag during FixedUpdate to prevent further collisions during that frame. You would then reset the flag during Update.

Comment
Add comment · Show 3 · 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 CgShady · Oct 19, 2014 at 02:06 PM 0
Share

Good idea, thanks! This works perfectly.

 if (collision.gameObject.tag != gameObject.tag || collision.gameObject.GetInstanceID() < gameObject.GetInstanceID())
             // handle collision
avatar image richyrich · Nov 12, 2014 at 06:21 PM 0
Share

If you could accept the answer, so others know the problem has been resolved, that would be cool. Glad it solved your problem

avatar image CgShady · Nov 13, 2014 at 10:14 PM 0
Share

sorry, I have a new account, it took me to log with my former account to do it.. Thanks again.

avatar image
0

Answer by DFLY · Oct 19, 2014 at 01:34 PM

What you can do is give each instance of the prefab a number one unit higher than the last prefab e.g.: "1 , 2 , 3 ...." Then use an if statement to make only the largest numbered prefab detect the collision. To number your prefabs to start with use something like:

 function InstantiateBlock(){
     
 blockNumber ++;
 blockNumberToString = blockNumber.ToString();
 newtBlock = Instantiate (tBlock, (transform.position + Vector3(0.0, 10.0, 0.0)), Quaternion.identity);
 newtBlock.name = blockNumberToString;
 blockName = newtBlock.name;
 thisBlock = GameObject.Find(blockName);
 thisBlock.rigidbody.velocity = Vector3 (0 , startVel , 0);
 }



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

28 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

Related Questions

Rigidbody colliders 1 Answer

My Character controller is being push a little bit when i'm about the get the coins. How can i possibly detect collision and destroy the coins without actually getting affected by the colliding physics? 1 Answer

rigid bodies passing through one another. 1 Answer

ExplosionForce On Instantiated Object 1 Answer

onTriggerEnter Won't Work 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