Unityの2Dげーむで爆弾オブジェクトを作り爆発と同時にその周辺にあるものを吹っ飛ばしたいです。
そのため以下のコードを用意しました。
吹っ飛ぶ方向は思い通りにいったのですが、爆発ボタン(ここでは"T")を押しても吹っ飛ばないことが多くあります。
(最終的には時間経過で爆発し、オブジェクトは消す予定ですがここでは実験のためボタンで起動し爆発後もオブジェクトが残るようにしています。)
うまくいくときもあるのでどこで躓いているのかわかりません。ご教授ください。
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Bomber : MonoBehaviour 6{ 7 public float coefficient; //空気毛抵抗係数 8 public float speed; //爆風の速さ 9 10 private void OnTriggerStay2D(Collider2D collision) 11 { 12 if (Input.GetKeyDown(KeyCode.T)) 13 { 14 Rigidbody2D colrigid2D = collision.GetComponent<Rigidbody2D>(); 15 if (colrigid2D == null) return; 16 //風速計算 17 var velocity = (collision.transform.position - transform.position).normalized * speed; 18 19 //風力を与える 20 colrigid2D.AddForce(coefficient * velocity); 21 } 22 } 23}
回答1件
あなたの回答
tips
プレビュー