Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PrefabScatterSpawner : MonoBehaviour
- {
- [System.Serializable]
- public struct SpawnInfo
- {
- public float frequency;
- public GameObject prefab;
- }
- public List<GameObject> spawnList = new List<GameObject>();
- public int minSpawnCount, maxSpawnCount;
- public Vector2 spawnArea = Vector2.one;
- [Range(0,1)]
- public float minSafeSpawnRadius= .5f;
- public float castStartHeight = 5;
- [Range(0, 5)]
- public float castOvershoot = 2;
- public int gizmoRaySamples = 4;
- public float sampleRate = 2;
- public LayerMask RaycastHitMask;
- [System.Serializable]
- public class GizmoColors
- {
- public Color areaColor = new Color(1f, 0.92f, 0.02f, 0.25f);
- public Color areaOvershootColor = new Color(1f, 0.56f, 0.02f, 0.25f);
- public Color raycastOriginColor = new Color(1f, 0f, 0f, 0.25f);
- public Color raycastHitColor = new Color(1f, 0f, 0f, 0.52f);
- public Color rayColor = new Color(0f, 1f, 0f, 0.25f);
- }
- [SerializeField] GizmoColors gizmoColors = new GizmoColors();
- // Start is called before the first frame update
- void Start()
- {
- }
- // Update is called once per frame
- void Update()
- {
- }
- private void OnValidate()
- {
- t = sampleRate;
- }
- public Vector3 GetRandomOffsetInSpawnArea()
- {
- float halfAreaY = spawnArea.y / 2;
- float halfAreaX = spawnArea.x / 2;
- return new Vector3(Random.Range(-halfAreaX, halfAreaX), 0, Random.Range(-halfAreaY, halfAreaY));
- }
- private float t = 0;
- private List<Vector3> samplePoints = new List<Vector3>();
- private void OnDrawGizmos()
- {
- Gizmos.color = gizmoColors.areaOvershootColor;
- Gizmos.DrawWireCube(transform.position + Vector3.up * castStartHeight / 2, new Vector3(spawnArea.x, castStartHeight, spawnArea.y));
- Gizmos.color = gizmoColors.areaColor;
- Gizmos.DrawWireCube(transform.position + Vector3.up * (castStartHeight - castOvershoot) / 2, new Vector3(spawnArea.x, castStartHeight + castOvershoot, spawnArea.y));
- Gizmos.color = gizmoColors.raycastOriginColor;
- Gizmos.DrawWireSphere(transform.position + Vector3.up * castStartHeight, .1f);
- foreach (var samplePoint in samplePoints)
- {
- Gizmos.color = gizmoColors.rayColor;
- Gizmos.DrawRay(samplePoint, Vector3.down * (castStartHeight + castOvershoot));
- if (Physics.Raycast(samplePoint, Vector3.down, out RaycastHit hit, castOvershoot + castStartHeight, RaycastHitMask))
- {
- Gizmos.color = gizmoColors.raycastHitColor;
- //Gizmos.DrawWireSphere(hit.point, .1f);
- CookieGizmo.DrawWireCircle(hit.point + Vector3.up * .01f, minSafeSpawnRadius, Vector3.up, Vector3.forward );
- }
- }
- t += Time.deltaTime;
- if (t < sampleRate)
- return;
- t -= sampleRate;
- samplePoints.Clear();
- for (int i = 0; i < gizmoRaySamples; i++)
- {
- for (int attempt = 0; attempt < 10; attempt++)
- {
- var randOffset = GetRandomOffsetInSpawnArea();
- var randomPosition = transform.position + Vector3.up * castStartHeight + randOffset;
- bool tooClose = false;
- foreach (var validPoints in samplePoints)
- {
- if (Vector3.Distance(randomPosition, validPoints) < minSafeSpawnRadius*2)
- {
- tooClose = true;
- break;
- }
- }
- if (!tooClose)
- {
- samplePoints.Add(randomPosition);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement