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 {
}
}
}