やりたいことなのですが、1つのGameObjectが別の複数個あるGameObjectに衝突したとき、OnCollisionEnterで衝突判定で取得したGameObjectをFixedJointを使用し接続させたいのです。
using
1using System.Collections.Generic; 2using UnityEngine; 3 4public class JointCount : MonoBehaviour { 5 6 // Use this for initialization 7 private List<GameObject> col = new List<GameObject>(); 8 int colCount = 0; 9 private List<FixedJoint> fixedJoints = new List<FixedJoint>(); 10 private FixedJoint fixedJoint; 11 12 void Start () { 13 14 } 15 16 // Update is called once per frame 17 void Update () { 18 19 } 20 void OnCollisionEnter(Collision collision) 21 { 22 if (collision.gameObject.tag == "Voxel" ) 23 { 24 gameObject.AddComponent<FixedJoint>();//自身にジョイントをアタッチ 25 //col.Add(collision.gameObject); 26 fixedJoint = GetComponent<FixedJoint>(); 27 fixedJoint.connectedBody = collision.gameObject.GetComponent<Rigidbody>(); 28 29 30 fixedJoints.Add(fixedJoint); 31 /*foreach (FixedJoint joint in fixedJoints) 32 { 33 fixedJoint = collision.gameObject.GetComponent<FixedJoint>(); 34 joint.connectedBody = collision.gameObject.GetComponent<Rigidbody>(); 35 36 }*/ 37 38 Debug.Log(""+fixedJoints[0]+gameObject.name); 39 40 41 } 42 43 } 44} 45コード
やっていることは、自動生成したGameObjectにtagをつけ、衝突したGameObjectがtagに設定したものなら自身にFixedJointをアタッチし、connectedBodyを衝突先に設定し接続するとしているものです。しかしこの状態だと衝突した数だけFixedJointのコンポーネントが増えるだけでconnectedBodyがNoneと出てしまいました。
衝突先やコンポーネントをlistで管理しようとしたのですが、私の書き方が悪いのかうまくいきませんでした。どのように直せばうまく動くのでしょうか、教えてくださると助かります。よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/02 08:04