private void PreparePlatform() { if (platform_collider_govar.go == null) { Debug.LogError("Curr Platform Collider Is Not Defined Yet!"); return; } if (map_govar.go == null) { Debug.LogError("Curr Platform Collider Is Not Defined Yet!"); return; } GameObject platform_collider = platform_collider_govar.go; // Reset Its Position Because It Could Have Been Moved By Previous Platform That Called This Script platform_collider.transform.position = new Vector3(0.0f, 0.0f, 0.0f); GOBounds platform_collider_gobounds = platform_collider.GetComponent <GOBounds>(); GameObject map = map_govar.go; Bounds map_bounds = map.GetComponent <GOBounds>().bounds; float x_pos_extent = map_bounds.extents.x; float y_pos_extent = map_bounds.extents.y; // Rotate Randomly platform_collider.transform.RotateAround(platform_collider.transform.position, new Vector3(0.0f, 0.0f, 1.0f), Random.Range(0.0f, 360.0f)); // Reset Bounds After Rotation platform_collider_gobounds.SetBounds(); // Make Random Extents Smaller So That Platofrm Collider Will Not Go Outside The Map x_pos_extent -= platform_collider_gobounds.bounds.extents.x; y_pos_extent -= platform_collider_gobounds.bounds.extents.y; // Get X and Y Positions Relative To Map Center float rand_x = Random.Range(-x_pos_extent, x_pos_extent); float rand_y = Random.Range(-y_pos_extent, y_pos_extent); // Get Map Center Vector3 map_bb_center = map_bounds.center; platform_collider.transform.position = new Vector3(map_bb_center.x + rand_x, map_bb_center.y + rand_y, platform_collider.transform.position.z); // Reset Bounds After Movement platform_collider_gobounds.SetBounds(); }
public void Prepare() { List <Collider2D> colliders = new List <Collider2D>(GetComponentsInChildren <Collider2D>()); GOBounds go_bounds = GetComponent <GOBounds>(); Vector3 pivot = new Vector3(0.0f, 0.0f, 0.0f); // Get Map's Bounds go_bounds.SetBounds(false); // Rotate its center around pivot on Z axis go_bounds.bounds.center = RotatePointAroundPivot(go_bounds.bounds.center, pivot, new Vector3(-90.0f, 0.0f, 0.0f)); // "Rotate" BB's Size by 90 along Z axis go_bounds.bounds.size = new Vector3(go_bounds.bounds.size.x, go_bounds.bounds.size.z, go_bounds.bounds.size.y); for (int i = 0; i < colliders.Count; i++) { GameObject map_go = colliders[i].gameObject; // Create placeholder and rotate it to XY Plane GameObject placeholder = Instantiate(placeholder_prefab, map_go.transform.position, map_go.transform.rotation, gameObject.transform); // Copy scale placeholder.transform.localScale = map_go.transform.localScale; // Rotate placeholder by 90 along Z axis placeholder.transform.RotateAround(pivot, new Vector3(1.0f, 0.0f, 0.0f), -90.0f); // Placeholders hold 2D Colliders and we need to modify their rotation correctly placeholder.transform.rotation = Quaternion.Euler(new Vector3(0.0f, 0.0f, map_go.transform.rotation.eulerAngles.y)); // During Rotation The Position on Z is messed up for some reason placeholder.transform.position = new Vector3(placeholder.transform.position.x, placeholder.transform.position.y, 0.0f); // Assign What Prefab This Placeholder Holds // string path = PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(map_go); GameObject prefab = AssetDatabase.LoadAssetAtPath <GameObject>(path); placeholder.GetComponent <InfrastructureData>().prefab = prefab; // Copy Collider2D Component From map_go // Collider2D compo = map_go.GetComponent <Collider2D>(); // Get The Component Of The Appropriate Derived Type ( derived from Collider2D ) Component placeholder_collider2D = placeholder.AddComponent(map_go.GetComponent <Collider2D>().GetType()); // Copy content from map_go Collider2D to placeholder_collider2D placeholder_collider2D.GetCopyOf(map_go.GetComponent <Collider2D>()); } }