//ifit is called ,damageDealer details private void ProcesHit(DamgeDealer dmgDealer) { health -= dmgDealer.GetDamage(); if (health <= 0) { Die(); } }
//reduce the heath if obstacle bullets collide with player //which has dameage dealer private void OnTriggerEnter2D(Collider2D otherObject) { //go in damage dealer class //reduce the health accordingly DamgeDealer dmgDealer = otherObject.gameObject.GetComponent <DamgeDealer>(); //if they object doesn not have a damgdelaer ignore it if (!dmgDealer) //if dmgdealer is { return; } ProcesHit(dmgDealer); GameObject explosion = Instantiate(explosionVfx, transform.position, Quaternion.identity); Destroy(explosion, explosionDuartion); //music when the player is hit AudioSource.PlayClipAtPoint(playerTouchedSound, Camera.main.transform.position, playerTouchedSoundVolume); }