When my code instantiate the 3 objects, they are always spawn with the wrong orientation and position (slightly off 0,0,0 for rotation, and few units off (0, posY, 0)). The rotation and position are only set in this code:
private GameObject root;
private List<GameObject> parts;
public GameObject capsule;
public GameObject fuelTank;
public GameObject engine;
public float jointBreakForce;
public float jointBreakTorque;
private Staging stagingScript;
// Use this for initialization
void Start () {
// get size of the items and place them as we go
float capsuleHeight = capsule.GetComponent<MeshFilter> ().sharedMesh.bounds.size.z;
float fuelTankHeight = fuelTank.GetComponent<MeshFilter> ().sharedMesh.bounds.size.z;
float engineHeight = engine.GetComponent<MeshFilter> ().sharedMesh.bounds.size.z;
GameObject capsuleI = (GameObject)Instantiate (capsule, new Vector3(0.0f,engineHeight+fuelTankHeight+capsuleHeight,0.0f), Quaternion.identity);
GameObject fuelTankI = (GameObject)Instantiate (fuelTank, new Vector3(0.0f,engineHeight+fuelTankHeight,0.0f), Quaternion.Euler(0.0f,0.0f,0.0f));
GameObject engineI = (GameObject)Instantiate (engine, new Vector3(0.0f, engineHeight, 0.0f), Quaternion.Euler(0.0f,0.0f,0.0f));
capsuleI.transform.parent = this.transform;
fuelTankI.transform.parent = this.transform;
engineI.transform.parent = this.transform;
parts = new List<GameObject> ();
// set capsule as root
root = capsuleI;
// add capsule to part list
parts.Add (capsuleI);
// add fuel tank
parts.Add (fuelTankI);
// add engine
parts.Add (engineI);
// connect the parts together with joints in sequential order
// error prevention code
if (parts.Count > 0) {
// start from 2nd item since we already have the first item
if (parts.Count > 1) {
GameObject prevPart = parts [0];
for (int i = 1; i < parts.Count; i++) {
Debug.Log(i);
Debug.Log(prevPart.name);
Debug.Log(parts*.name);*
-
FixedJoint joint = prevPart.AddComponent<FixedJoint>();*
-
joint.breakForce = jointBreakForce;*
-
joint.breakTorque = jointBreakTorque;*
-
joint.enableCollision = true;*
_ joint.connectedBody = parts*.GetComponent();*_
* Debug.Log(joint);*
* Debug.Break();*
_ prevPart = parts*;
}
}
}*_
* capsuleI.transform.rotation = Quaternion.identity;*
* fuelTankI.transform.rotation = Quaternion.identity;*
* engineI.transform.rotation = Quaternion.identity;*
* stagingScript = transform.GetComponent ();*
* // a hack to get demo decoupling working*
* stagingScript.Decoupler1 = root.GetComponent ();*
* stagingScript.Decoupler1Parent = stagingScript.Decoupler1.GetComponent();*
* stagingScript.Decoupler1Child = stagingScript.Decoupler1.connectedBody;*
* }*
* // Update is called once per frame*
* void Update () {*
* }*
Also, the prefabs are all set to 0,0,0 for orientation and position, as well as the parent with they are spawned as children to.
I’m I missing something about instantiating prefabs? or is the Joint spawning code the problem?