Пример #1
0
    private tankType type;             //发射子弹的坦克类型
    void OnCollisionEnter(Collision other)
    {
        MyFactory      mF        = Singleton <MyFactory> .Instance;
        ParticleSystem explosion = mF.GetPs();                                             //获取爆炸的粒子系统

        explosion.transform.position = transform.position;                                 //设置粒子系统位置
        Collider[] colliders = Physics.OverlapSphere(transform.position, explosionRadius); //获取爆炸范围内的所有碰撞体
        for (int i = 0; i < colliders.Length; i++)
        {
            if (colliders[i].tag == "tankPlayer" && this.type == tankType.Enemy ||
                colliders[i].tag == "tankEnemy" && this.type == tankType.Player)                        //发射子弹的坦克类型和被击中的坦克类型不同,才造成伤害
            {
                float distance = Vector3.Distance(colliders[i].transform.position, transform.position); //被击中坦克与爆炸中心的距离
                float hurt     = 100f / distance;                                                       //受到的伤害
                float current  = colliders[i].GetComponent <Tank>().GetHp();                            //当前的生命值
                colliders[i].GetComponent <Tank>().SetHp(current - hurt);                               //设置受伤后生命值
            }
        }
        explosion.Play();        //播放爆炸的粒子系统
        if (this.gameObject.activeSelf)
        {
            mF.RecycleBullet(this.gameObject);            //回收子弹
        }
    }