void Start() { // Get the marker image in the scene. if (string.IsNullOrEmpty(Lead_Marker_Name)) { return; } GameObject markerObject = GameObject.Find(Lead_Marker_Name); if (markerObject) { markerImage = markerObject.GetComponent <Image>(); } else { // The marker cannot be found in the scene. Debug.LogWarning(Lead_Marker_Name + " cannot be found in the scene."); Destroy(this); return; } markerTransform = markerImage.transform; // Get the "Aiming_Control_CS" in the tank. aimingScript = GetComponent <Aiming_Control_CS>(); if (aimingScript == null) { Debug.LogWarning("'Aiming_Control_CS' cannot be found in the MainBody."); Destroy(this); } // Get the "Bullet_Generator_CS". if (Bullet_Generator_Script == null) { Bullet_Generator_Script = GetComponentInChildren <Bullet_Generator_CS>(); } if (Bullet_Generator_Script == null) { Debug.LogWarning("'Bullet_Generator_CS' cannot be found. The cannon cannot get the bullet velocity."); Destroy(this); return; } bulletGeneratorTransform = Bullet_Generator_Script.transform; }
void Initialize() { // This function must be called in Start() after changing the hierarchy. thisTransform = transform; parentTransform = thisTransform.parent; aimingScript = GetComponentInParent <Aiming_Control_CS>(); currentLocalAngles = thisTransform.localEulerAngles; angleY = currentLocalAngles.y; Max_Right = angleY + Max_Right; Max_Left = angleY - Max_Left; // Get the "Bullet_Generator_CS". if (Bullet_Generator_Script == null) { Bullet_Generator_Script = GetComponentInChildren <Bullet_Generator_CS>(); } if (Bullet_Generator_Script == null) { Debug.LogWarning("'Bullet_Generator_CS' cannot be found. The cannon cannot get the bullet velocity."); // Set the fake value. bulletVelocity = 250.0f; } }
void Create() { Transform oldTransform = thisTransform.Find("Barrel"); // Find the old object. int childCount; Transform[] childTransforms; if (oldTransform) { childCount = oldTransform.transform.childCount; childTransforms = new Transform [childCount]; for (int i = 0; i < childCount; i++) { childTransforms [i] = oldTransform.GetChild(0); // Get the child object such as "Armor_Collider". childTransforms [i].parent = thisTransform; // Change the parent of the child object. } DestroyImmediate(oldTransform.gameObject); // Delete old object. } else { childCount = 0; childTransforms = null; } // Create new Gameobject & Set Transform. GameObject newObject = new GameObject("Barrel"); newObject.transform.parent = thisTransform; newObject.transform.localPosition = -thisTransform.localPosition + new Vector3(Offset_XProp.floatValue, Offset_YProp.floatValue, Offset_ZProp.floatValue); newObject.transform.localRotation = Quaternion.identity; // Mesh settings. MeshRenderer meshRenderer = newObject.AddComponent <MeshRenderer> (); Material[] materials = new Material [Materials_NumProp.intValue]; for (int i = 0; i < materials.Length; i++) { materials [i] = MaterialsProp.GetArrayElementAtIndex(i).objectReferenceValue as Material; } meshRenderer.materials = materials; MeshFilter meshFilter = newObject.AddComponent <MeshFilter> (); meshFilter.mesh = Part_MeshProp.objectReferenceValue as Mesh; // Collider settings. for (int i = 0; i < Colliders_NumProp.intValue; i++) { MeshCollider meshCollider = newObject.AddComponent <MeshCollider>(); meshCollider.sharedMesh = Colliders_MeshProp.GetArrayElementAtIndex(i).objectReferenceValue as Mesh; meshCollider.convex = true; } // Add "Damage_Control_01_Turret_CS" script. if (Use_Damage_ControlProp.boolValue) { var damageScript = newObject.AddComponent <Damage_Control_02_Turret_CS>(); // Get the "Turret_Index" value in the "Turret_Base_CS" script. Turret_Base_CS turretScript = thisTransform.parent.GetComponentInChildren <Turret_Base_CS>(); if (turretScript) { Turret_IndexProp.intValue = turretScript.Turret_Index; damageScript.Turret_Index = Turret_IndexProp.intValue; } } // Set the layer. newObject.layer = 0; // Return the child objects. if (childCount > 0) { for (int i = 0; i < childCount; i++) { childTransforms [i].transform.parent = newObject.transform; } } // Send the "Barrel_Type" value. Recoil_Brake_CS recoilScript = thisTransform.GetComponent <Recoil_Brake_CS> (); if (recoilScript) { recoilScript.Barrel_Type = Barrel_TypeProp.intValue; } Bullet_Generator_CS generatorScript = thisTransform.GetComponentInChildren <Bullet_Generator_CS> (); if (generatorScript) { generatorScript.Barrel_Type = Barrel_TypeProp.intValue; } }