Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 juliaqiuxy · Apr 30, 2017 at 05:59 PM · collisioncolliderspherecollider

Collider ran twice when the same script is attached to both gameobjects

I am working on a project where users would instantiate rain droplets at random location(using the same prefab), and while two rain droplets collide with each other, one of them is destroyed and the one that stays grows in scale.

There is a logical flaw in the code I have, which every rain droplet has a sphere collider and with the same code attached. When two rain droplets collide with each other, the same code will run twice which means both raindrops are destroyed.

My temporary hack to this is to compare two rain droplets' y position and the one with larger y stays. However, this will not scale because later on, I will be adding wind and changing their scale, x-axis and z-axis.

Is there a better approach to this problem? Can I use an empty object as all rain droplets' parent and detect the collision only once?

Thanks a lot!

     void OnTriggerEnter(Collider other) {
         Vector3 updatedScale = new Vector3 ((transform.localScale.x + other.transform.localScale.x), (transform.localScale.y + other.transform.localScale.y), (transform.localScale.z + other.transform.localScale.z));
         Vector3 updatedLocation = new Vector3((transform.position.x + other.transform.position.x)/2, (transform.position.y + other.transform.position.y)/2, (transform.position.z + other.transform.position.z)/2);
 
         if (this.tag == "sm" && other.gameObject.tag == "sm" ) {
             if (this.transform.position.y > other.transform.position.y) {
                 Debug.Log ("test-other");
                 Destroy(other.gameObject);
                 this.gameObject.transform.position = updatedLocation;
                 this.gameObject.transform.localScale = updatedScale;
             } else if (this.transform.localScale.y < other.transform.localScale.y) {
                 //Destroy (gameObject);
             } else {
             }
         }
     }
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

2 Replies

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

Answer by oStaiko · Apr 30, 2017 at 07:44 PM

Unity assigns a unique ID to all gameobjects (GetInstanceID()). Just compare these two, and destroy the one with the smaller ID value.

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 juliaqiuxy · May 01, 2017 at 03:22 AM 0
Share

Awesome! Works perfectly. Thanks a lot!

avatar image
0

Answer by tMahon · May 01, 2017 at 04:33 AM

You could also use the Observer pattern here. Every time a raindrop collides with another it sets off an event that tells a Game Manager that it collided with another droplet, including its own transform in the call. Both colliders will send their responses and destroy themselves. Each time the event is triggered check a static counter for the count being an even number, and instantiate a new object at double scale at the location of the last triggered event. It seems like overkill for a small problem but it opens up a management level that could easily implement a scorekeeper, an achievement system, etc.

 public class RainDropManager : MonoBehaviour {
 
     private void RainDropCollisionEventHandler(Transform t) {
         if (RainDrop.RainDropCount % 2 == 0)
             Instantiate(t.gameObject, t.position, Quaternion.identity, t);
     }
 
     private void OnEnable() {
         RainDrop.RainDropCollisionEvent += RainDropCollisionEventHandler;
     }
 }
 
 public delegate void RainDropCollisionDelegate(Transform selfTransform);
 
 public class RainDrop : MonoBehaviour {
 
     public static event RainDropCollisionDelegate RrainDropCollisionEvent;
     public static int RainDropCount{get;set;}
 
     private void OnTriggerEnter2D(Collider2D collision) {
 
         if (collision.tag == "RainDrop")
         {
             RainDropCount++;
             RrainDropCollisionEvent.Invoke(transform);
             Destroy(gameObject);
         }
     }
 }

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 juliaqiuxy · May 01, 2017 at 04:42 AM 0
Share

Super smart! Never thought of using the modular. $$anonymous$$uch appreciated!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem about collisions between the cubes. 2 Answers

Collision with cloth not working,Cloth component not working 0 Answers

How to get the points of 2 collided objects 0 Answers

How do I “freeze” an object after a collision? 3 Answers

Camera Collision Not Working 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