private void OnTriggerEnter2D(Collider2D collision) { if (collision.transform.tag == "enemybullet" || collision.transform.tag == "bossbullet")//与玩家的子弹发生碰撞 { float damage = collision.GetComponent <EnemyBullet>().powerEnemyBullet + EnemyBullet.addPowerEnemyBullet; this.currentHealth -= damage; //生命值-敌方子弹伤害 Game.ReduceHealth(damage, health); //改变血条 Destroy(collision.gameObject); //碰撞后子弹消失 } if (collision.transform.tag == "healthbox") { if (this.currentHealth + collision.GetComponent <HealthBox>().health <= health) { this.currentHealth += collision.GetComponent <HealthBox>().health;//生命值+盒子携带的生命值 //lifeBar.fillAmount += collision.GetComponent<HealthBox>().health / health;//改变血条 Game.AddHealth(collision.GetComponent <HealthBox>().health, health); Destroy(collision.gameObject);//碰撞后盒子消失 } else if (this.currentHealth + collision.GetComponent <HealthBox>().health >= health) { this.currentHealth = health; //lifeBar.fillAmount = 1f; Game.FillHealth(); Destroy(collision.gameObject);//碰撞后盒子消失 } } if (currentHealth <= 0) { Destroy(gameObject); //飞机爆炸 StartGame.selfPlane.isAlive = false; Instantiate(boom, transform.position, Quaternion.identity); //显示爆炸动画 } }
//检测碰撞 private void OnTriggerEnter2D(Collider2D collision) { if (collision.transform.tag == "bullet") //与玩家的子弹发生碰撞 { this.health -= collision.GetComponent <SelfBullet>().powerSelfBullet; //生命值-子弹伤害 Destroy(collision.gameObject); //碰撞后子弹消失 } if (collision.transform.tag == "Player") { float damage = health / 2; collision.GetComponent <PlaneController>().currentHealth -= damage; Game.ReduceHealth(damage, collision.GetComponent <PlaneController>().health); this.health = 0; } }