###前提・実現したいこと
UnityでC#を使ってスクリプトを書いています。
以下のコードでは、子オブジェクトにHingeJointをComponentしていますが、孫オブジェクトにComponentする場合、どのようにコードを変えればいいのかわかりません。
なるべく形式を変えずに孫オブジェクトを取得し、HingeJointをComponentさせる方法があれば、教えていただきたいです。
###該当のソースコード
C#
1using UnityEngine; 2using System.Collections; 3 4 5public class Alpha3 : MonoBehaviour 6{ 7 8 void Start() 9 { 10 int count = 0; 11 GameObject[] childObject = new GameObject[4]; 12 13 //Search child objects 14 foreach (Transform child in gameObject.transform) 15 { 16 childObject[count] = gameObject.transform.FindChild(child.name).gameObject; 17 count++; 18 19 } 20 21 AttachHingeJointA(childObject[0], childObject[1], childObject[2], childObject[3]); 22 23 } 24 25 26 public void AttachHingeJointA(GameObject block0, GameObject block1, GameObject block2, GameObject block3) 27 { 28 29 //Connect 0&1 30 HingeJoint hinge01 = block0.AddComponent<HingeJoint>(); 31 32 hinge01.anchor = new Vector3(-0.5f, 0.0f, 0.5f); 33 hinge01.axis = new Vector3(0.0f, 1.0f, 0.0f); 34 hinge01.connectedBody = block1.GetComponent<Rigidbody>(); 35 36 //Connect 1&2 37 HingeJoint hinge12 = block1.AddComponent<HingeJoint>(); 38 39 hinge12.anchor = new Vector3(0.5f, 0.0f, -0.5f); 40 hinge12.axis = new Vector3(0.0f, 1.0f, 0.0f); 41 hinge12.connectedBody = block2.GetComponent<Rigidbody>(); 42 43 //Connect 2&3 44 HingeJoint hinge23 = block2.AddComponent<HingeJoint>(); 45 46 hinge23.anchor = new Vector3(0.5f, 0.0f, -0.5f); 47 hinge23.axis = new Vector3(0.0f, 1.0f, 0.0f); 48 hinge23.connectedBody = block3.GetComponent<Rigidbody>(); 49 50 //Connect 3&0 51 HingeJoint hinge30 = block3.AddComponent<HingeJoint>(); 52 53 hinge30.anchor = new Vector3(-0.5f, 0.0f, 0.5f); 54 hinge30.axis = new Vector3(0.0f, 1.0f, 0.0f); 55 hinge30.connectedBody = block0.GetComponent<Rigidbody>(); 56 } 57 58 59}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/27 03:18