void OnCollisionEnter2D(Collision2D collision) { GameObject collided = collision.gameObject; Zombie zombie = collided.GetComponent <Zombie>(); // Did we hit a zombie? if (null != zombie) { audioSource.PlayOneShot(zombieSound, 0.2f); zombie.BulletHit(); } else { audioSource.PlayOneShot(wallSound, 0.2f); BuildableWall wall = collided.GetComponent <BuildableWall>(); // Did we hit a buildable wall? if (null != wall) { wall.BulletHit(); } } /* Disable rigidbody and boxcollider, so the bullet effectively disappears, * but the sound will keep playing */ SpriteRenderer rend = GetComponent <SpriteRenderer>(); BoxCollider2D bc = GetComponent <BoxCollider2D>(); rend.enabled = false; Destroy(bc); // Finally, destroy the bullet after 1s Destroy(gameObject, 1f); }
void OnCollisionEnter2D(Collision2D collision) { GameObject collided = collision.gameObject; Zombie zombie = collided.GetComponent <Zombie>(); // Did we hit a zombie? if (null != zombie) { audioSource.PlayOneShot(zombieSound, 0.2f); zombie.BulletHit(); } else { // Play a sound audioSource.PlayOneShot(wallSound, 0.2f); // Did we hit a destructible wall? if (collided.transform.parent != null) { if (collided.transform.parent.gameObject.tag == "DestructibleBlock") { // Destroy the smaller sub-block that was hit Destroy(collided.transform.gameObject); } } } /* Disable rigidbody and boxcollider, so the bullet effectively disappears, * but the sound will keep playing */ SpriteRenderer rend = GetComponent <SpriteRenderer>(); BoxCollider2D bc = GetComponent <BoxCollider2D>(); rend.enabled = false; Destroy(bc); // Finally, destroy the bullet after 1s Destroy(gameObject, 1f); }