AddExplosionForce
爆発のエフェクトをシミュレートするためにRigidbodyに力を適用させます。
爆発力はRigidbodyからの距離に応じて直線的に減少していきます
Unity公式
AddExplosionForce(float 強さ, Vector3 場所, float 範囲);
注意点
爆風で飛ばしたい全てのオブジェクトにRigidbodyが必要です。
ソースコード
public float pow = 1.0f; public float radius = 3.0f; public GameObject effect; Rigidbody rb; Ray ray; RaycastHit hit; void Update() { if (Input.GetMouseButtonDown(0)) { //クリックした場所にレイを飛ばす ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit, 100f)) { //クリックした付近のコライダーを取得 Collider[] cols = Physics.OverlapSphere(hit.point, 3f); foreach (Collider target in cols) { //範囲内のゲームオブジェクト全てのRigidbodyにAddExplosionForceする if (target.GetComponent<Rigidbody>()) { target.GetComponent<Rigidbody>().AddExplosionForce(pow, hit.point, radius); } } //爆破パーティクル Instantiate(effect, hit.point, Quaternion.identity); } } }
コメント
[…] Rigidbody で爆風を表現する | のっぴの備忘録 […]