欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

unity3d 增加子弹碰撞后物体的爆炸力

程序员文章站 2024-03-16 19:42:22
...
using UnityEngine;

public class Bullet : MonoBehaviour {

    public GameObject bulletEffecte;
    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        Destroy(gameObject,3f);
    }
    void OnCollisionEnter(Collision e)
    {
        Instantiate(bulletEffecte,transform.position,transform.rotation);
        Destroy(gameObject);

        if(e.gameObject.tag =="Cube")  //如果目标的标签是cube
        {
            //将球形的(OverlapSphere)的物理引擎(Physics) 放在一个碰撞体的数组里
            Collider[] collider = Physics.OverlapSphere(transform.position,2);  
           //便利这个数组
            foreach (Collider c in collider)
            {
                Rigidbody r = c.GetComponent<Rigidbody>(); //获得这个数组里的元素的刚体
                if(r != null)    //如果刚体不是空的
                {
                    r.AddExplosionForce(1000, transform.position, 3);//增加爆炸力(AddExplosionForce) (爆炸的力,爆炸点,爆炸范围)
                }
            }

        }
    }
}
相关标签: unity3d