Unity物理引擎开发介绍_刚体爆破效果

 

下面是个刚体爆炸的物理效果模拟,可以应用到手雷效果,下面是代码实现

 

        [SerializeField] private float radius;  //作用半径
        [SerializeField] private float power;  //作用力
        [SerializeField] private int damage; //伤害
        [SerializeField] private GameObject effect; //伤害特效
        [SerializeField] private float timer; // 作用时间

 

  void Update()
        {
            timer -= Time.deltaTime;
            if (timer <= 0)
            {
                Vector3 explosionPosition = transform.position;
                Collider[] colliders = Physics.OverlapSphere(explosionPosition, radius);
                Instantiate(effect, explosionPosition, Quaternion.identity);
                for (int i = 0; i < colliders.Length; i++)
                {
                    IHealth health = colliders[i].gameObject.GetComponent<IHealth>();
                    if (health != null)
                    {
                        health.TakeDamage(damage);
                    }
                    Rigidbody rb = colliders[i].GetComponent<Rigidbody>();
                    if (rb != null)
                    {
                        rb.AddExplosionForce(power, explosionPosition, radius, 3.0f);
                    }
                }
                Destroy(gameObject);
            }
        }

 

 

如有错误,大家可以一起交流

 联系方式 qq: 940299880

你可能感兴趣的:(unity3d逻辑代码使用方法,unity,物理引擎)