Created
June 3, 2025 07:40
-
-
Save Mike-Schvedov/b9774b3884d94aa1b94980032e6b406b to your computer and use it in GitHub Desktop.
ProceduralGenerator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| public class ProceduralGenerator : MonoBehaviour | |
| { | |
| public GameObject prefab; | |
| public int numberOfPrefabInstances = 200; | |
| public Vector3 generationAreaSize = new Vector3(100f, 1f, 100f); | |
| public float yAxisOffset = 0.07f; | |
| public Transform parentContainer; | |
| void Start() | |
| { | |
| // If no parentContainer provided, instances will be generated as children of the generator. | |
| if (parentContainer == null) | |
| { | |
| parentContainer = transform.root; | |
| } | |
| gameObject.transform.position = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z); | |
| Generate(); | |
| } | |
| void Generate() | |
| { | |
| for (int i = 0; i < numberOfPrefabInstances; i++) | |
| { | |
| Vector3 randomPosition = GetRandomPositionInGenerationArea(); | |
| Quaternion randomRotation = Quaternion.Euler(0f, Random.Range(0f, 360f), 0f); | |
| //Instantiate(prefab, randomPosition, randomRotation); | |
| Instantiate(prefab, randomPosition, randomRotation, parentContainer.transform); | |
| } | |
| } | |
| Vector3 GetRandomPositionInGenerationArea() | |
| { | |
| Vector3 randomPosition = new Vector3( | |
| Random.Range(-generationAreaSize.x / 2, generationAreaSize.x / 2), | |
| 0f, | |
| Random.Range(-generationAreaSize.z / 2, generationAreaSize.z / 2) | |
| ); | |
| // you can adjust the Y coordinate based on terrain height or other criteria | |
| randomPosition.y = yAxisOffset; | |
| return transform.position + randomPosition; | |
| } | |
| // Draw Gizmo to visualize the generation area | |
| void OnDrawGizmosSelected() | |
| { | |
| Gizmos.color = Color.green; | |
| Gizmos.DrawWireCube(transform.position, generationAreaSize); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment