using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Crate : MonoBehaviour
{
public GameObject fracturedCrate;
public GameObject explosionEffect;// Start is called before the first frame updatevoidStart(){}// Update is called once per framevoidUpdate(){if(Input.GetKeyDown(KeyCode.Space)){Instantiate(explosionEffect, transform.position, Quaternion.identity);// 生成爆炸效果,同样位置,保持不旋转//Instantiate(fracturedCrate, transform.position, Quaternion.identity);// 生成碎片箱子,在同样的位置,保持不旋转
GameObject fracturedCrateObj =Instantiate(fracturedCrate, transform.position, Quaternion.identity);// 生成碎片箱子的游戏对象,并赋值到一个变量
Rigidbody[] allRigidBodies = fracturedCrateObj.GetComponentsInChildren<Rigidbody>();// 获取所有子对象的刚体,注意这里 Components 的 s 不能少了!// 放入一个列表中if(allRigidBodies.Length >0)// 判断成功获取了刚体{foreach(var body in allRigidBodies){
body.AddExplosionForce(1000, transform.position,1);// 施加一个爆炸的力量,1000牛顿,位置就在当前位置,辐射半径为 1}}Destroy(gameObject);// 消除整体箱子}}}