public void OnCollisionEnter2D(Collision2D collision) { GameObject o = collision.gameObject; GameObject pc = GameObject.FindWithTag("Player"); ShootControllerBase shootController = pc.GetComponent <PlayerShootController>(); if (o.CompareTag("Enemy") && Shooter.Equals(pc)) { o.GetComponentInParent <Enemy>().Health--; } else if (o.CompareTag("Player") && !Shooter.Equals(pc)) { shootController = Shooter.GetComponent <EnemyShootController>(); o.GetComponentInParent <Player>().Health--; } if (shootController.BulletCollideClips.Any()) { var clipToPlay = shootController.BulletCollideClips[Random.Range(0, shootController.BulletCollideClips.Count)]; clipToPlay.pitch = Random.Range(shootController.MinBulletCollidePitch, shootController.MaxBulletCollidePitch); clipToPlay.Play(); } Destroy(gameObject); }
// Bullet collides with something and handles health public void OnCollisionEnter2D(Collision2D collision) { GameObject characterAttacked = collision.gameObject; player = GameObject.FindWithTag("Player"); ShootControllerBase shootController = player.GetComponent <PlayerShootController>(); if (characterAttacked.CompareTag("Enemy") && _shooter.Equals(player)) { int damage = player.GetComponent <Player>()._damage; int actualDamage = 1 + (damage / 2); characterAttacked.GetComponentInParent <Enemy>().Health -= actualDamage; } else if (characterAttacked.CompareTag("Player") && !_shooter.Equals(player)) { shootController = _shooter.GetComponent <EnemyShootController>(); if (!characterAttacked.GetComponentInParent <Player>()._invulnerable) { characterAttacked.GetComponentInParent <Player>().Health--; } } if (shootController.BulletCollideClips.Any()) { var clipToPlay = shootController.BulletCollideClips[Random.Range(0, shootController.BulletCollideClips.Count)]; clipToPlay.pitch = Random.Range(shootController.MinBulletCollidePitch, shootController.MaxBulletCollidePitch); clipToPlay.Play(); } Destroy(gameObject); }