Advertisement
maus234

ObjectGenerator

Apr 14th, 2024
18,609
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.91 KB | Gaming | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.Collections;
  4.  
  5. public class ObjectGenerator : MonoBehaviour
  6. {
  7.     [System.Serializable]
  8.     public class ObjectToGenerate
  9.     {
  10.         public GameObject Prefab;
  11.         public int NumberOfObjects;
  12.         public float MinScale = 0.8f; // Minimum scale of the object
  13.         public float MaxScale = 1.2f; // Maximum scale of the object
  14.         public bool RandomRotation = false; // Should the object have a random rotation?
  15.     }
  16.  
  17.     public List<ObjectToGenerate> ObjectsToGenerate; // Assign your objects and quantities in the inspector
  18.     public GameObject PointAObject; // Assign your Point A GameObject in the inspector
  19.     public GameObject PointBObject; // Assign your Point B GameObject in the inspector
  20.     public float MinDistanceBetweenObjects = 2f; // Minimum distance between objects
  21.     public GameObject ReferenceObject; // The object on which generated objects should spawn
  22.     public GameObject BushPrefab; // Assign your bush prefab in the inspector
  23.     public float MinDistanceBetweenBushes = 13f; // Minimum distance between bushes
  24.     public float MaxDistanceBetweenBushes = 20f; // Maximum distance between bushes
  25.  
  26.     private List<GameObject> generatedObjects;
  27.  
  28.     void Start()
  29.     {
  30.         generatedObjects = new List<GameObject>();
  31.         foreach (var objectToGenerate in ObjectsToGenerate)
  32.         {
  33.             GenerateObjects(objectToGenerate);
  34.         }
  35.         GenerateBushesAlongReferenceObject();
  36.     }
  37.     void GenerateBushesAlongReferenceObject()
  38.     {
  39.         Collider2D referenceCollider = ReferenceObject.GetComponent<Collider2D>();
  40.         if (referenceCollider == null)
  41.         {
  42.             Debug.LogError("ReferenceObject does not have a Collider2D component.");
  43.             return;
  44.         }
  45.  
  46.         Bounds bounds = referenceCollider.bounds;
  47.         float currentX = bounds.min.x;
  48.         bool firstBush = true; // Flag to track if the bush is the first one being spawned
  49.  
  50.         while (currentX < bounds.max.x)
  51.         {
  52.             if (firstBush)
  53.             {
  54.                 firstBush = false; // Ensure the first bush spawns at the very beginning
  55.             }
  56.             else
  57.             {
  58.                 float randomDistance = Random.Range(MinDistanceBetweenBushes, MaxDistanceBetweenBushes);
  59.                 currentX += randomDistance;
  60.             }
  61.  
  62.             if (currentX <= bounds.max.x)
  63.             {
  64.                 Vector2 spawnPosition = new Vector2(currentX, bounds.min.y + 1);
  65.                 GameObject bush = Instantiate(BushPrefab, spawnPosition, Quaternion.identity);
  66.                 generatedObjects.Add(bush);
  67.             }
  68.         }
  69.     }
  70.  
  71.     void GenerateObjects(ObjectToGenerate objectToGenerate)
  72.     {
  73.         Vector2 center = (PointAObject.transform.position + PointBObject.transform.position) / 2;
  74.         Vector2 size = new Vector2(Mathf.Abs(PointAObject.transform.position.x - PointBObject.transform.position.x),
  75.                                    Mathf.Abs(PointAObject.transform.position.y - PointBObject.transform.position.y));
  76.  
  77.         float objectHeight = GetPrefabHeight(objectToGenerate.Prefab);
  78.  
  79.         for (int i = 0; i < objectToGenerate.NumberOfObjects; i++)
  80.         {
  81.             Vector2 position = Vector2.zero;
  82.             bool positionFound = false;
  83.  
  84.             while (!positionFound)
  85.             {
  86.                 position = new Vector2(Random.Range(center.x - size.x / 2, center.x + size.x / 2), center.y);
  87.                 position = FindSpawnPoint(position, objectHeight);
  88.                 positionFound = true;
  89.  
  90.                 foreach (GameObject otherObject in generatedObjects)
  91.                 {
  92.                     if (Vector2.Distance(otherObject.transform.position, position) < MinDistanceBetweenObjects)
  93.                     {
  94.                         positionFound = false;
  95.                         break;
  96.                     }
  97.                 }
  98.             }
  99.  
  100.             GameObject newObj = Instantiate(objectToGenerate.Prefab, new Vector3(position.x, position.y, 0), Quaternion.identity);
  101.             generatedObjects.Add(newObj);
  102.  
  103.             float scale = Random.Range(objectToGenerate.MinScale, objectToGenerate.MaxScale);
  104.             newObj.transform.localScale = new Vector3(scale, scale, scale);
  105.  
  106.             if (objectToGenerate.RandomRotation)
  107.             {
  108.                 float rotation = Random.Range(0f, 360f);
  109.                 newObj.transform.eulerAngles = new Vector3(0f, 0f, rotation);
  110.             }
  111.         }
  112.     }
  113.  
  114.     private Vector2 FindSpawnPoint(Vector2 spawnPosition, float objectHeight)
  115.     {
  116.  
  117.         Collider2D referenceCollider = ReferenceObject.GetComponent<Collider2D>();
  118.         if (referenceCollider != null)
  119.         {
  120.             Bounds bounds = referenceCollider.bounds;
  121.             // If the pivot is at the bottom of the prefab, we add the full objectHeight
  122.             // This will place the bottom of the prefab at the top of the reference object
  123.             float spawnY = bounds.max.y + objectHeight - 5.7f;
  124.  
  125.             if (spawnPosition.x >= bounds.min.x && spawnPosition.x <= bounds.max.x)
  126.             {
  127.                 // Return the x position and a y position that's been adjusted to be on top of the reference object
  128.                 return new Vector2(spawnPosition.x, spawnY);
  129.             }
  130.         }
  131.         //Debug.LogError("ReferenceObject does not have a Collider2D component.");
  132.         return spawnPosition; // Fallback to the original position if no collider is found
  133.     }
  134.  
  135.  
  136.  
  137.     private float GetPrefabHeight(GameObject prefab)
  138.     {
  139.         SpriteRenderer spriteRenderer = prefab.GetComponent<SpriteRenderer>();
  140.         if (spriteRenderer != null)
  141.         {
  142.             return spriteRenderer.sprite.bounds.size.y;
  143.         }
  144.         else
  145.         {
  146.             Debug.LogError("Prefab does not have a SpriteRenderer component.");
  147.             return 0f;
  148.         }
  149.     }
  150. }
  151.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement