Ну, вам нужно как-то переслать другие коллизии.
Например, сохранить коллизии OnCollisionEnter
и удалить их OnCollisionExit
.Если все объекты делают это, вы можете получить коллизии всех объектов, с которыми вы сейчас сталкиваетесь и т. Д.
Может выглядеть, например,
public class CollisionDetection : MonoBehaviour
{
// List for storing current collisions
// (the references of the Collisions component of colliding objects to be exact)
public List<CollisionDetection> collisions = new List<CollisionDetection>();
private void OnCollisionEnter(Collision other)
{
// however you want to check the collisions
if(other.tag != "XY") return;
// Try to get the CollisionDetection component
// Note depending on your collider setup you might
// have to use GetComponentInChildren or GetComponentInParent instead
var collComponent = other.gameObject.GetComponent<CollisionDetection>();
// If no Collisions component found do nothing
if(!collComponent) return;
// If this Collisions component is already in the list do nothing
if(collisions.Contains(collComponent)) return;
// Add the reference to the list
collisions.Add(collComponent);
// probably some check if you want to call destroy
if(! .... ) return;
// Careful this should be called only by one of the objects
DestroyCollisions(new List<CollisionDetection>(){this});
}
private void OnCollisionExit(Collision other)
{
// however you want to check the collisions
if(other.tag != "XY") return;
// Try to get the CollisionDetection component
// same as before you might have to use
// GetComponentInChildren or GetComponentInParent
var collComponent = other.gameObject.GetComponent<CollisionDetection>();
// If no Collisions component found do nothing
if(!collComponent) return;
// If list doesn't contain the reference do nothing
if(!collisions.Contains(collComponent)) return;
// Remove reference from the list
collisions.Remove(collComponent);
}
// pass a parameter in order to not destroy the original callers objects to early
public void DestroyCollisions(List<CollisionDetection> originalCallers)
{
// Now when you destroy objects check for other collisions recursiv
foreach(var collision in collisions)
{
// Don't destroy the original callers since they will destroy themselves when done
if(originalCallers.Contains(collision)) continue;
// it is possible that two objects collide with the same other object
// so they try to destroy the same object twice -> exception
// So if one reference is null already skip
if(!collision) continue;
// Maybe another check? E.g. is color equal? etc
if(! ...) continue;
// Add to original callers to not destroy it to early
originalCallers.Add(collision);
// Destroy also this collision's collisions
collision.DestroyCollisions(originalCallers);
}
// Finally destroy this object itself
Destroy(this.gameObject);
}
}
Как вы заполняете эти теги if
, делаяВсе звонки безопасны и т. д. Ваша задача.
Никаких гарантий, хотя, поскольку я взломал это на своем смартфоне;), но я надеюсь, что вы поняли
Обновление
Чтобы не уничтожать объекты, а просто собрать их в списке, вы можете сделать
public List<CollisionDetection> FetchCollisions()
{
var output = new List<CollisionDetection>();
output.Add(this);
// check for other collisions recursiv
foreach(var collision in collisions)
{
foreach(var col in collision.FetchCollisions())
{
if(output.Contains(col)) continue;
output.Add(col);
}
}
}