protected void PlaceObjects(GameObject[] listOfObjects, int objectsCount, List <Vector2> positions, bool alignToGround = false) { for (int i = 0; i < objectsCount; i++) { Vector3 centralPosition = transform.position; // Pick one type of building - some buildings could be placed only once in the village! GameObject objectToPlace = listOfObjects[UnityEngine.Random.Range(0, listOfObjects.Length)]; // Set position and rotation of the building in three steps: // get one position in x, and y dimensions: Vector2 position2D = positions[i]; int currentTerrainIndex = MapScript.GetTerrainForCoordinates(position2D.x, position2D.y); float objectToPlaceY = Terrain.activeTerrains[currentTerrainIndex].SampleHeight(new Vector3(centralPosition.x + position2D.x, 0, centralPosition.z + position2D.y)); objectToPlaceY += Terrain.activeTerrain.GetPosition().y; objectToPlaceY += 0.01f * i; // set position of the new object relative to the group center (parent transform): Vector3 desiredPosition = new Vector3(centralPosition.x + position2D.x, objectToPlaceY, centralPosition.z + position2D.y); // first, set random horizontal rotation along Y axis, then rotate verticaly acording to the ground: GameObject newObject = Instantiate(objectToPlace, desiredPosition, Quaternion.identity, transform); newObject.transform.Rotate(0f, UnityEngine.Random.Range(0, 350), 0f); if (alignToGround) { newObject.transform.rotation = AlignToGround.Align(newObject.transform); } } }
private void RandomizeTreesMaterialsInForest() { for (int i = 0; i < _trees.Length; i++) { Material randomMaterial = _treesMaterials[UnityEngine.Random.Range(0, _treesMaterials.Length)]; for (int j = 0; j < _trees[i].transform.childCount; j++) { MeshRenderer[] renderers = _trees[i].GetComponentsInChildren <MeshRenderer>(); for (int k = 0; k < renderers.Length; k++) { if (!renderers[k].CompareTag("Trunk") || renderers[k].sharedMaterial.Equals(randomMaterial)) { renderers[k].sharedMaterial = randomMaterial; } } } AlignToGround.Align(_trees[i].transform); } }