void Set_Types_With_SwingBalls(float radius) { // called only in Parent Type. Transform parentTransform = Selection.activeGameObject.transform; int count = 0; for (int i = 0; i < parentTransform.childCount; i++) { Transform childTransform = parentTransform.GetChild(i); PTS_Static_Track_CS childScript = childTransform.GetComponent <PTS_Static_Track_CS> (); if (childScript) { Vector3 childRotaion = childTransform.localEulerAngles; if (childRotaion.y < 0.0f) { childRotaion.y = 360.0f + childRotaion.y; } if (childRotaion.y > 150.0f && childRotaion.y < 210.0f) // Flat Upper Piece. { if (Set_Closest_SwingBall(childTransform, radius)) { count += 1; } else if (childScript.Type == 1) { childScript.Type = 2; childScript.Anchor_Transform = null; childScript.Anchor_Name = null; childScript.Anchor_Parent_Name = null; } } } } Debug.Log(count + " pieces could find SwingBalls as their Anchors."); }
void Set_Types_With_RoadWheels(float radius) { // called only in Parent Type. Transform parentTransform = Selection.activeGameObject.transform; int count = 0; for (int i = 0; i < parentTransform.childCount; i++) { Transform childTransform = parentTransform.GetChild(i); PTS_Static_Track_CS childScript = childTransform.GetComponent <PTS_Static_Track_CS> (); if (childScript) { if (Set_Closest_DrivingWheel(childTransform, radius)) { count += 1; } else if (childScript.Type == 1) { childScript.Type = 2; childScript.Anchor_Transform = null; childScript.Anchor_Name = null; childScript.Anchor_Parent_Name = null; } } } Debug.Log(count + " pieces could find RoadWheels as their Anchors."); }
void Set_Parent_Script(GameObject newObject) { // Set 'Static_Track_CS' script as parent. PTS_Static_Track_CS trackScript = newObject.AddComponent <PTS_Static_Track_CS> (); trackScript.Type = 9; // Parent ; trackScript.Length = Collider_InfoProp.boundsValue.size.z; trackScript.Stored_Body_Mass = thisTransform.parent.GetComponent <Rigidbody> ().mass + (Track_MassProp.floatValue * thisTransform.childCount); // to increase the MainBody's Mass. PTS_Drive_Control_CS driveControlScript = thisTransform.parent.GetComponent <PTS_Drive_Control_CS> (); if (driveControlScript) { trackScript.Stored_Torque = driveControlScript.Torque * 1.25f; // to increase Torque. } // Store suspension angles. PTS_Create_RoadWheel_CS [] wheelParentScripts = thisTransform.parent.GetComponentsInChildren <PTS_Create_RoadWheel_CS> (); RoadWheelsProp[] roadWheelsPropArray = new RoadWheelsProp [wheelParentScripts.Length]; for (int i = 0; i < wheelParentScripts.Length; i++) { roadWheelsPropArray [i] = wheelParentScripts [i].Get_Current_Angles(); } trackScript.RoadWheelsProp_Array = roadWheelsPropArray; // Change children's parent. int childCount = thisTransform.childCount; for (int i = 0; i < childCount; i++) { Transform childTransform = thisTransform.GetChild(0); childTransform.parent = newObject.transform; } }
void Make_Upper_Pieces_Static() { // called only in Parent Type. Transform parentTransform = Selection.activeGameObject.transform; int count = 0; for (int i = 0; i < parentTransform.childCount; i++) { Transform childTransform = parentTransform.GetChild(i); PTS_Static_Track_CS childScript = childTransform.GetComponent <PTS_Static_Track_CS> (); if (childScript) { Vector3 childRotaion = childTransform.localEulerAngles; if (childRotaion.y < 0.0f) { childRotaion.y = 360.0f + childRotaion.y; } if (childRotaion.y > 90.0f && childRotaion.y < 270.0f) // Upper Piece. { childScript.Type = 0; childScript.Anchor_Transform = null; childScript.Anchor_Name = null; childScript.Anchor_Parent_Name = null; count += 1; } } } Debug.Log(count + " pieces have been set to Static."); }
void Initial_Settings() { // Set initial position and angle. invisiblePos = thisTransform.localPosition; invisibleAngY = thisTransform.localEulerAngles.y; // Set direction. isLeft = (invisiblePos.y > 0.0f); // Set invertValue. if (invisibleAngY > 90.0f && invisibleAngY < 270.0f) // Upper piece { invertValue = 180.0f; } else // Lower piece { invertValue = 0.0f; } // Set the reference scripts. if (Front_Transform && Front_Script == null) { Front_Script = Front_Transform.GetComponent <PTS_Static_Track_CS> (); } if (Rear_Transform && Rear_Script == null) { Rear_Script = Rear_Transform.GetComponent <PTS_Static_Track_CS> (); } // Set simpleFlag. switch (Type) { case 1: // Anchor if (Front_Script.Type == 1 && Rear_Script.Type == 1) { simpleFlag = false; } else if (Front_Script.Type == 1 && (Front_Script.Anchor_Parent_Name + Front_Script.Anchor_Name) != (Anchor_Parent_Name + Anchor_Name)) { simpleFlag = false; } else if (Rear_Script.Type == 1 && (Rear_Script.Anchor_Parent_Name + Rear_Script.Anchor_Name) != (Anchor_Parent_Name + Anchor_Name)) { simpleFlag = false; } else { simpleFlag = true; } break; case 2: // Dynamic if (Front_Script.Type == 2 && Rear_Script.Type == 2) { simpleFlag = true; } break; } }
void Copy_To_Opposite(Transform thisTransform, int type) { Transform oppositeTransform; bool isLeft = thisTransform.localPosition.y > 0.0f; if (isLeft) //Left { oppositeTransform = thisTransform.parent.Find(thisTransform.name.Replace("_L", "_R")); } else //Right { oppositeTransform = thisTransform.parent.Find(thisTransform.name.Replace("_R", "_L")); } if (oppositeTransform) { PTS_Static_Track_CS oppositeScript = oppositeTransform.GetComponent <PTS_Static_Track_CS> (); oppositeScript.Type = type; // Set Type. // Set Anchor. if (type == 1) { PTS_Static_Track_CS thisScript = thisTransform.GetComponent <PTS_Static_Track_CS> (); if (thisScript.Anchor_Transform) { string oppositeAnchorParentName = thisScript.Anchor_Parent_Name; string oppositeAnchorName = thisScript.Anchor_Name; if (isLeft) //Left { oppositeAnchorName = oppositeAnchorName.Replace("_L", "_R"); } else //Right { oppositeAnchorName = oppositeAnchorName.Replace("_R", "_L"); } Transform oppositeAnchorTransform = oppositeTransform.parent.parent.Find(oppositeAnchorParentName + "/" + oppositeAnchorName); if (oppositeAnchorTransform) { oppositeScript.Anchor_Transform = oppositeAnchorTransform; oppositeScript.Anchor_Parent_Name = oppositeAnchorParentName; oppositeScript.Anchor_Name = oppositeAnchorName; } else { Debug.LogWarning("Opposite Anchor cannot be found. " + thisTransform.name); } } } } }
void Start() { // Do not change to "Awake ()". Because "Awake ()" is called while creating the Static_Track. thisTrackScript = GetComponent <PTS_Static_Track_CS> (); thisMeshFilter = GetComponent <MeshFilter> (); mesh_A = thisMeshFilter.mesh; mesh_B = thisTrackScript.Front_Transform.GetComponent <MeshFilter> ().mesh; currentMesh = mesh_A; // Set direction. if (transform.localPosition.y > 0.0f) { isLeft = true; // Left } else { isLeft = false; // Right } }
void Switch_Both_Sides_Types(Transform pieceTransform) { // If both sides of Anchor piece are Static, change to Dynamic. PTS_Static_Track_CS pieceScript = pieceTransform.GetComponent <PTS_Static_Track_CS> (); Transform frontPiece = pieceScript.Front_Transform; Transform rearPiece = pieceScript.Rear_Transform; PTS_Static_Track_CS frontScript = frontPiece.GetComponent <PTS_Static_Track_CS> (); PTS_Static_Track_CS rearScript = rearPiece.GetComponent <PTS_Static_Track_CS> (); if (frontScript.Type == 0) // Static >> { frontScript.Type = 2; // >> Dynamic Copy_To_Opposite(frontPiece, 2); } if (rearScript.Type == 0) // Static >> { rearScript.Type = 2; // >> Dynamic Copy_To_Opposite(rearPiece, 2); } }
void Use_StoredValues() { PTS_Static_Track_CS staticTrackScript = thisTransform.parent.GetComponentInChildren <PTS_Static_Track_CS> (); if (staticTrackScript) { if (staticTrackScript.RoadWheelsProp_Array.Length == 0) { Debug.LogWarning("The values for Static_Track are not stored. Please set 'Angle' and 'SphereCollider Radius' manually."); return; } for (int i = 0; i < staticTrackScript.RoadWheelsProp_Array.Length; i++) { if (staticTrackScript.RoadWheelsProp_Array [i].parentName == thisTransform.name) { // Set Radius. CapsuleCollider [] capsuleColliders = staticTrackScript.GetComponentsInChildren <CapsuleCollider> (); for (int j = 0; j < capsuleColliders.Length; j++) { if (capsuleColliders [j].enabled == false) { Wheel_RadiusProp.floatValue = staticTrackScript.RoadWheelsProp_Array [i].baseRadius + capsuleColliders [j].radius * 2.0f; break; } } // Set Angles. Set_IndividuallyProp.boolValue = true; Sus_AnglesProp.arraySize = NumProp.intValue; for (int j = 0; j < NumProp.intValue; j++) { Sus_AnglesProp.GetArrayElementAtIndex(j).floatValue = staticTrackScript.RoadWheelsProp_Array [i].angles [j]; } break; } } } else { Debug.LogWarning("Static_Track cannot be found in this tank."); return; } }
void Make_All_Pieces_Static() { // called only in Parent Type. Transform parentTransform = Selection.activeGameObject.transform; int count = 0; for (int i = 0; i < parentTransform.childCount; i++) { Transform childTransform = parentTransform.GetChild(i); PTS_Static_Track_CS childScript = childTransform.GetComponent <PTS_Static_Track_CS> (); if (childScript) { childScript.Type = 0; childScript.Anchor_Transform = null; childScript.Anchor_Name = null; childScript.Anchor_Parent_Name = null; count += 1; } } Debug.Log(count + " pieces have been set to Static."); }
void Prepare_With_Static_Track(PTS_Static_Track_CS tempScript) { // Called from "PTS_Static_Track_CS". staticTrackScript = tempScript; if (staticTrackScript.Reference_L && staticTrackScript.Reference_R) { // Set rate. float radius = GetComponent <MeshFilter> ().mesh.bounds.extents.x + Radius_Offset; if (isLeft) // Left { staticTrackRate = staticTrackScript.Reference_Radius_L / radius; } else // Right { staticTrackRate = staticTrackScript.Reference_Radius_R / radius; } } else { Debug.LogWarning("Static_Wheel can not find the reference wheel in the Static_Tracks."); Destroy(this); } }
bool Set_Closest_DrivingWheel(Transform pieceTransform, float radius) { Transform bodyTransform = pieceTransform.parent.parent; PTS_Drive_Wheel_CS [] driveScripts = bodyTransform.GetComponentsInChildren <PTS_Drive_Wheel_CS> (); float minDist = radius; Transform closestWheel = null; for (int i = 0; i < driveScripts.Length; i++) { Transform connectedTransform = driveScripts [i].GetComponent <HingeJoint> ().connectedBody.transform; if (connectedTransform != bodyTransform) // should be connected to Suspension { float tempDist = Vector3.Distance(pieceTransform.position, driveScripts [i].transform.position); // Distance to the piece. if (tempDist < minDist) { closestWheel = driveScripts [i].transform; minDist = tempDist; } } } if (closestWheel) { EditorApplication.delayCall += () => { PTS_Static_Track_CS pieceScript = pieceTransform.GetComponent <PTS_Static_Track_CS>(); pieceScript.Type = 1; pieceScript.Anchor_Transform = closestWheel; pieceScript.Anchor_Parent_Name = closestWheel.parent.name; pieceScript.Anchor_Name = closestWheel.name; Switch_Both_Sides_Types(pieceTransform); }; return(true); } else { return(false); } }
bool Set_Closest_SwingBall(Transform pieceTransform, float radius) { Transform bodyTransform = pieceTransform.parent.parent; PTS_Create_SwingBall_CS [] ballScripts = bodyTransform.GetComponentsInChildren <PTS_Create_SwingBall_CS> (); float minDist = radius; Transform closestBall = null; for (int i = 0; i < ballScripts.Length; i++) { ConfigurableJoint[] joints = ballScripts [i].GetComponentsInChildren <ConfigurableJoint> (); for (int j = 0; j < joints.Length; j++) { float tempDist = Vector3.Distance(pieceTransform.position, joints [j].transform.position); // Distance to the piece. if (tempDist < minDist) { closestBall = joints [j].transform; minDist = tempDist; } } } if (closestBall) { EditorApplication.delayCall += () => { PTS_Static_Track_CS pieceScript = pieceTransform.GetComponent <PTS_Static_Track_CS>(); pieceScript.Type = 1; pieceScript.Anchor_Transform = closestBall; pieceScript.Anchor_Parent_Name = closestBall.parent.name; pieceScript.Anchor_Name = closestBall.name; Switch_Both_Sides_Types(pieceTransform); }; return(true); } else { return(false); } }
void Set_Children_References() { Transform parentTransform = Selection.activeGameObject.transform; PTS_Static_Track_CS[] childScripts = parentTransform.GetComponentsInChildren <PTS_Static_Track_CS> (); for (int i = 0; i < childScripts.Length; i++) { PTS_Static_Track_CS childScript = childScripts [i]; if (childScript.Type != 9) // Not Parent. { childScript.Front_Script = childScript.Front_Transform.GetComponent <PTS_Static_Track_CS> (); childScript.Rear_Script = childScript.Rear_Transform.GetComponent <PTS_Static_Track_CS> (); // if (childScript.Anchor_Transform == null) // Anchor_Transform is lost. // Find Anchor with reference to the name. { if (string.IsNullOrEmpty(childScript.Anchor_Name) == false && string.IsNullOrEmpty(childScript.Anchor_Parent_Name) == false) { childScript.Anchor_Transform = parentTransform.parent.Find(childScript.Anchor_Parent_Name + "/" + childScript.Anchor_Name); } } } } }
void Set_Child_Script() { // Called from "Create_TrackBelt_CSEditor". // Add "Static_Track_CS" and set values, and disable it. PTS_Static_Track_CS trackScript = gameObject.AddComponent <PTS_Static_Track_CS> (); trackScript.Front_Transform = frontTransform; trackScript.Rear_Transform = rearTransform; switch (type) { case 0: // Static if (frontTransform.GetComponent <PTS_Static_Track_Setting_CS> ().type == 1) // The front piece is Anchor type. { trackScript.Type = 2; // >> Dynamic } else if (rearTransform.GetComponent <PTS_Static_Track_Setting_CS> ().type == 1) // The rear piece is Anchor type. { trackScript.Type = 2; // >> Dynamic } else { trackScript.Type = 0; // Static } break; case 1: // Anchor trackScript.Type = 1; // Anchor trackScript.Anchor_Name = anchorName; trackScript.Anchor_Parent_Name = anchorParentName; break; case 2: // Dynamic trackScript.Type = 2; // Dynamic break; } // Add "Static_Track_Switch_Mesh_CS". if (Use_2ndPiece) { gameObject.AddComponent <PTS_Static_Track_Switch_Mesh_CS> (); } // Remove needless components. HingeJoint hingeJoint = GetComponent <HingeJoint> (); if (hingeJoint) { DestroyImmediate(hingeJoint); } Rigidbody rigidbody = GetComponent <Rigidbody> (); if (rigidbody) { DestroyImmediate(rigidbody); } PTS_Stabilizer_CS stabilizerScript = GetComponent <PTS_Stabilizer_CS> (); if (stabilizerScript) { DestroyImmediate(stabilizerScript); } // Disable CapsuleCollider. CapsuleCollider [] capsuleColliders = GetComponents <CapsuleCollider> (); for (int i = 0; i < capsuleColliders.Length; i++) { capsuleColliders [i].enabled = false; } // Remove child objects such as Reinforce piece. for (int i = 0; i < transform.childCount; i++) { GameObject childObject = transform.GetChild(i).gameObject; if (childObject.layer == PTS_Layer_Settings_CS.Reinforce_Layer) // Reinforce. { DestroyImmediate(childObject); } } }
void Prepare_With_Static_Track(PTS_Static_Track_CS tempScript) { // Called from parent's "PTS_Static_Track_CS". parentScript = tempScript; }
void Prepare_With_Static_Track(PTS_Static_Track_CS tempScript) { // Called from parent's "PTS_Static_Track_CS". parentScript = tempScript; halfLength = parentScript.Length * 0.5f; }