void OnCollisionEnter2D(Collision2D collision) { bool damagePlayer = false; // Collision with enemy EnemyScript enemy = collision.gameObject.GetComponent <EnemyScript>(); if (enemy) { // Kill the enemy HealthScript enemyHealth = enemy.GetComponent <HealthScript>(); if (enemyHealth) { enemyHealth.Damage(enemyHealth.hp); } //make explosion enemyHealth.ExplosionAnimation(explosionPrefab); SoundEffectsHelper.Instance.MakeDamageSound(); damagePlayer = true; } // Damage the player if (damagePlayer) { HealthScript playerHealth = GetComponent <HealthScript>(); if (playerHealth) { playerHealth.Damage(1); } } // Collision with area shot ammo AreaAmmoScript ammo = collision.gameObject.GetComponent <AreaAmmoScript>(); if (ammo) { AreaWeaponScript weapon = GetComponent <AreaWeaponScript>(); if (weapon) { weapon.ammunition += 1; ammoCounter.IncreaseCounter(); } Destroy(ammo.gameObject); } // Collision with time stop TimeStopScript timeStop = collision.gameObject.GetComponent <TimeStopScript>(); if (timeStop) { // disable box collider BoxCollider2D bc = timeStop.gameObject.GetComponent <BoxCollider2D>(); if (bc) { bc.enabled = false; } // disable renderer SpriteRenderer sr = timeStop.gameObject.GetComponent <SpriteRenderer>(); if (sr) { sr.enabled = false; } timeStop.StopTime(); } }
void OnTriggerEnter2D(Collider2D otherCollider) { //is this a heart? LifeItemScript lifeItem = otherCollider.gameObject.GetComponent <LifeItemScript>(); if (lifeItem != null) { if (!isEnemy) { hp++; Destroy(otherCollider.gameObject); } } // Is this a shot? ShotScript shot = otherCollider.gameObject.GetComponent <ShotScript>(); if (shot != null) { // Avoid friendly fire if (shot.isEnemyShot != isEnemy) { Damage(shot.damage); // Destroy the shot Destroy(shot.gameObject); // Remember to always target the game object, otherwise you will just remove the script if (!shot.isEnemyShot) { ScoreScript.scoreValue += 10; } } return; } HomingShotScript homingShot = otherCollider.gameObject.GetComponent <HomingShotScript>(); if (homingShot != null) { Debug.Log("homing shot hit!"); // Avoid friendly fire if (isEnemy) { Damage(homingShot.damage); // Destroy the shot Destroy(homingShot.gameObject); // Remember to always target the game object, otherwise you will just remove the script ScoreScript.scoreValue += 10; } return; } else { Debug.Log("homing shot not hit!"); } //is this an item? TimeStopScript item = otherCollider.gameObject.GetComponent <TimeStopScript>(); if (item != null && isEnemy == false) { item.StopTime(); Destroy(item.gameObject); return; } }