Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- public class HybridSpawner : MonoBehaviour
- {
- public GameObject[] arrayChildren;
- public enum SpawnMode { staticPoint, staticLine, staticCircle, randomCircle, randomSphere}
- public SpawnMode SpawnShape;
- public enum LookAt { lookAtCentre, lookAtSpawner, customRotation }
- public LookAt LookAtMode;
- [Tooltip("The total amount of children spawned from the spawner.")]
- public int cloneCount;
- [Header("Tip: You can rotate the spawner without rotating the children.")]
- [Tooltip("For Static Point, this will offset the position the children spawn from the spawner. For Static Line, this will offset the position the children spawn from each other.")]
- public Vector3 setPosition;
- [Tooltip("Rotates each child individually. If left as 0, it will be the child's forward vector.")]
- public Vector3 setRotation;
- private float staticCircleX;
- [Tooltip("For Circle and Sphere spawn shapes, this will offset what height the children spawn from the spawner.")]
- public float staticCircleY;
- private float staticCircleZ;
- [Tooltip("For Circle and Sphere spawn shapes, this will scale the radius of which the children spawn from the spawner. Default Radius is 1 unit.")]
- public float spawnRadiusScale = 1f;
- [Tooltip("For random spawn shapes, this sets the maximum angle a spawned child can be rotated at spawn.")]
- public Vector3 randomRotMax;
- [Header("Lock Look Rotation")]
- [Tooltip("Locks the X rotation to the set rotation when using 'Look at Centre' or 'Look at Spawner'.")]
- public bool dontLookX;
- [Tooltip("Locks the Y rotation to the set rotation when using 'Look at Centre' or 'Look at Spawner'.")]
- public bool dontLookY;
- [Tooltip("Locks the Z rotation to the set rotation when using 'Look at Centre' or 'Look at Spawner'.")]
- public bool dontLookZ;
- void Awake()
- {
- if (SpawnShape == SpawnMode.staticPoint)
- {
- for (int i = 0; i < cloneCount; i++)
- {
- Vector3 spawnPosition = setPosition;
- if (LookAtMode == LookAt.lookAtCentre)
- {
- Vector3 direction = (transform.position - setPosition);
- direction.y = 0;
- if (dontLookX)
- {
- direction.x = setRotation.x;
- }
- if (dontLookY)
- {
- direction.y = setRotation.y;
- }
- if (dontLookZ)
- {
- direction.z = setRotation.z;
- }
- Quaternion lookDirection = Quaternion.LookRotation(direction);
- spawnPosition += new Vector3(0, staticCircleY, 0);
- GameObject clone = Instantiate(arrayChildren[Random.Range(0, arrayChildren.Length)], transform.TransformPoint(spawnPosition), lookDirection);
- clone.transform.parent = transform;
- }
- if (LookAtMode == LookAt.lookAtSpawner)
- {
- Vector3 direction = (transform.position - setPosition);
- direction.y = 0;
- if (dontLookX)
- {
- direction.x = setRotation.x;
- }
- if (dontLookY)
- {
- direction.y = setRotation.y;
- }
- if (dontLookZ)
- {
- direction.z = setRotation.z;
- }
- Quaternion lookDirection = Quaternion.LookRotation(direction);
- GameObject clone = Instantiate(arrayChildren[Random.Range(0, arrayChildren.Length)], transform.TransformPoint(spawnPosition), lookDirection);
- clone.transform.parent = transform;
- }
- if (LookAtMode == LookAt.customRotation)
- {
- if(setRotation == Vector3.zero)
- {
- setRotation = Vector3.forward;
- }
- Quaternion lookDirection = Quaternion.LookRotation(setRotation);
- GameObject clone = Instantiate(arrayChildren[Random.Range(0,arrayChildren.Length)], transform.TransformPoint(spawnPosition), lookDirection);
- clone.transform.parent = transform;
- }
- }
- }
- if(SpawnShape == SpawnMode.staticLine)
- {
- for (int i = 0; i < cloneCount; i++)
- {
- Vector3 spawnPosition = setPosition * i;
- if (LookAtMode == LookAt.lookAtCentre)
- {
- Vector3 direction = (transform.position - setPosition);
- direction.y = 0;
- if (dontLookX)
- {
- direction.x = setRotation.x;
- }
- if (dontLookY)
- {
- direction.y = setRotation.y;
- }
- if (dontLookZ)
- {
- direction.z = setRotation.z;
- }
- Quaternion lookDirection = Quaternion.LookRotation(direction);
- spawnPosition += new Vector3(0, staticCircleY, 0);
- GameObject clone = Instantiate(arrayChildren[Random.Range(0, arrayChildren.Length)], transform.TransformPoint(spawnPosition), lookDirection);
- clone.transform.parent = transform;
- }
- if (LookAtMode == LookAt.lookAtSpawner)
- {
- Vector3 direction = (transform.position - setPosition);
- direction.y = 0;
- if (dontLookX)
- {
- direction.x = setRotation.x;
- }
- if (dontLookY)
- {
- direction.y = setRotation.y;
- }
- if (dontLookZ)
- {
- direction.z = setRotation.z;
- }
- Quaternion lookDirection = Quaternion.LookRotation(direction);
- GameObject clone = Instantiate(arrayChildren[Random.Range(0, arrayChildren.Length)], transform.TransformPoint(spawnPosition), lookDirection);
- clone.transform.parent = transform;
- }
- if (LookAtMode == LookAt.customRotation)
- {
- if (setRotation == Vector3.zero)
- {
- setRotation = Vector3.forward;
- }
- Quaternion lookDirection = Quaternion.LookRotation(setRotation);
- GameObject clone = Instantiate(arrayChildren[Random.Range(0, arrayChildren.Length)], transform.TransformPoint(spawnPosition), lookDirection);
- clone.transform.parent = transform;
- }
- }
- }
- if (SpawnShape == SpawnMode.staticCircle)
- {
- for (int i = 0; i < cloneCount; i++)
- {
- float theta = ((2 * Mathf.PI) / cloneCount) * i;
- float staticCircleX = Mathf.Cos(theta);
- float staticCircleZ = Mathf.Sin(theta);
- Vector3 positionAroundCircle = new Vector3(staticCircleX * spawnRadiusScale, 0, staticCircleZ * spawnRadiusScale);
- if (LookAtMode == LookAt.lookAtCentre)
- {
- Vector3 direction = transform.position - positionAroundCircle;
- if (dontLookX)
- {
- direction.x = setRotation.x;
- }
- if (dontLookY)
- {
- direction.y = setRotation.y;
- }
- if (dontLookZ)
- {
- direction.z = setRotation.z;
- }
- Quaternion lookDirection = Quaternion.LookRotation(direction);
- positionAroundCircle += new Vector3(0, staticCircleY, 0);
- GameObject clone = Instantiate(arrayChildren[Random.Range(0, arrayChildren.Length)], transform.TransformPoint(positionAroundCircle), lookDirection);
- clone.transform.parent = transform;
- }
- if (LookAtMode == LookAt.lookAtSpawner)
- {
- positionAroundCircle += new Vector3(0, staticCircleY, 0);
- Vector3 direction = transform.position - positionAroundCircle;
- if (dontLookX)
- {
- direction.x = setRotation.x;
- }
- if (dontLookY)
- {
- direction.y = setRotation.y;
- }
- if (dontLookZ)
- {
- direction.z = setRotation.z;
- }
- Quaternion lookDirection = Quaternion.LookRotation(direction);
- GameObject clone = Instantiate(arrayChildren[Random.Range(0, arrayChildren.Length)], transform.TransformPoint(positionAroundCircle), lookDirection);
- clone.transform.parent = transform;
- }
- if (LookAtMode == LookAt.customRotation)
- {
- if (setRotation == Vector3.zero)
- {
- setRotation = Vector3.forward;
- }
- Quaternion lookDirection = Quaternion.LookRotation(setRotation);
- GameObject clone = Instantiate(arrayChildren[Random.Range(0, arrayChildren.Length)], transform.TransformPoint(positionAroundCircle), lookDirection);
- clone.transform.parent = transform;
- }
- }
- }
- if (SpawnShape == SpawnMode.randomCircle)
- {
- for (int i = 0; i < cloneCount; i++)
- {
- Vector2 onUnitCircle = Random.insideUnitCircle.normalized;
- Vector3 randomPosition = new Vector3(onUnitCircle.x * spawnRadiusScale, 0, onUnitCircle.y * spawnRadiusScale);
- if (LookAtMode == LookAt.lookAtCentre)
- {
- Vector3 direction = transform.position - randomPosition;
- if (dontLookX)
- {
- direction.x = setRotation.x;
- }
- if (dontLookY)
- {
- direction.y = setRotation.y;
- }
- if (dontLookZ)
- {
- direction.z = setRotation.z;
- }
- Quaternion lookDirection = Quaternion.LookRotation(direction);
- randomPosition += new Vector3(0, staticCircleY, 0);
- GameObject clone = Instantiate(arrayChildren[Random.Range(0, arrayChildren.Length)], transform.TransformPoint(randomPosition), lookDirection);
- clone.transform.parent = transform;
- }
- if (LookAtMode == LookAt.lookAtSpawner)
- {
- randomPosition += new Vector3(0, staticCircleY, 0);
- Vector3 direction = transform.position - randomPosition;
- if (dontLookX)
- {
- direction.x = setRotation.x;
- }
- if (dontLookY)
- {
- direction.y = setRotation.y;
- }
- if (dontLookZ)
- {
- direction.z = setRotation.z;
- }
- Quaternion lookDirection = Quaternion.LookRotation(direction);
- GameObject clone = Instantiate(arrayChildren[Random.Range(0, arrayChildren.Length)], transform.TransformPoint(randomPosition), lookDirection);
- clone.transform.parent = transform;
- }
- if (LookAtMode == LookAt.customRotation)
- {
- if (randomRotMax == Vector3.zero)
- {
- randomRotMax = Vector3.one * 360;
- }
- if (setRotation == Vector3.zero)
- {
- setRotation = Vector3.forward;
- }
- Quaternion randomRotation = Quaternion.LookRotation(new Vector3(Random.Range(0, randomRotMax.x), Random.Range(0, randomRotMax.y), Random.Range(0, randomRotMax.z)));
- randomPosition += new Vector3(0, staticCircleY, 0);
- GameObject clone = Instantiate(arrayChildren[Random.Range(0, arrayChildren.Length)], transform.TransformPoint(randomPosition), randomRotation);
- clone.transform.parent = transform;
- }
- }
- }
- if (SpawnShape == SpawnMode.randomSphere)
- {
- for (int i = 0; i < cloneCount; i++)
- {
- Vector3 randomPosition = new Vector3(Random.insideUnitSphere.x * spawnRadiusScale, Random.insideUnitSphere.y * spawnRadiusScale, Random.insideUnitSphere.z * spawnRadiusScale);
- if (LookAtMode == LookAt.lookAtCentre)
- {
- Vector3 direction = transform.position - randomPosition;
- if (dontLookX)
- {
- direction.x = setRotation.x;
- }
- if (dontLookY)
- {
- direction.y = setRotation.y;
- }
- if (dontLookZ)
- {
- direction.z = setRotation.z;
- }
- Quaternion lookDirection = Quaternion.LookRotation(direction);
- randomPosition += new Vector3(0, staticCircleY, 0);
- GameObject clone = Instantiate(arrayChildren[Random.Range(0, arrayChildren.Length)], transform.TransformPoint(randomPosition), lookDirection);
- clone.transform.parent = transform;
- }
- if (LookAtMode == LookAt.lookAtSpawner)
- {
- randomPosition += new Vector3(0, staticCircleY, 0);
- Vector3 direction = transform.position - randomPosition;
- if (dontLookX)
- {
- direction.x = setRotation.x;
- }
- if (dontLookY)
- {
- direction.y = setRotation.y;
- }
- if (dontLookZ)
- {
- direction.z = setRotation.z;
- }
- Quaternion lookDirection = Quaternion.LookRotation(direction);
- GameObject clone = Instantiate(arrayChildren[Random.Range(0, arrayChildren.Length)], transform.TransformPoint(randomPosition), lookDirection);
- clone.transform.parent = transform;
- }
- if (LookAtMode == LookAt.customRotation)
- {
- if(randomRotMax == Vector3.zero)
- {
- randomRotMax = Vector3.one * 360;
- }
- if (setRotation == Vector3.zero)
- {
- setRotation = Vector3.forward;
- }
- Quaternion randomRotation = Quaternion.LookRotation(new Vector3(Random.Range(0, randomRotMax.x), Random.Range(0, randomRotMax.y), Random.Range(0, randomRotMax.z)));
- GameObject clone = Instantiate(arrayChildren[Random.Range(0, arrayChildren.Length)], transform.TransformPoint(randomPosition), randomRotation);
- clone.transform.parent = transform;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement