Advertisement
hybrid-dragon

HybridSpawner

Jan 30th, 2018
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.13 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class HybridSpawner : MonoBehaviour
  4. {
  5.     public GameObject[] arrayChildren;
  6.     public enum SpawnMode { staticPoint, staticLine, staticCircle, randomCircle, randomSphere}
  7.     public SpawnMode SpawnShape;
  8.     public enum LookAt { lookAtCentre, lookAtSpawner, customRotation }
  9.     public LookAt LookAtMode;
  10.     [Tooltip("The total amount of children spawned from the spawner.")]
  11.     public int cloneCount;
  12.     [Header("Tip: You can rotate the spawner without rotating the children.")]
  13.     [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.")]
  14.     public Vector3 setPosition;
  15.     [Tooltip("Rotates each child individually. If left as 0, it will be the child's forward vector.")]
  16.     public Vector3 setRotation;
  17.     private float staticCircleX;
  18.     [Tooltip("For Circle and Sphere spawn shapes, this will offset what height the children spawn from the spawner.")]
  19.     public float staticCircleY;
  20.     private float staticCircleZ;
  21.     [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.")]
  22.     public float spawnRadiusScale = 1f;
  23.     [Tooltip("For random spawn shapes, this sets the maximum angle a spawned child can be rotated at spawn.")]
  24.     public Vector3 randomRotMax;
  25.     [Header("Lock Look Rotation")]
  26.     [Tooltip("Locks the X rotation to the set rotation when using 'Look at Centre' or 'Look at Spawner'.")]
  27.     public bool dontLookX;
  28.     [Tooltip("Locks the Y rotation to the set rotation when using 'Look at Centre' or 'Look at Spawner'.")]
  29.     public bool dontLookY;
  30.     [Tooltip("Locks the Z rotation to the set rotation when using 'Look at Centre' or 'Look at Spawner'.")]
  31.     public bool dontLookZ;
  32.  
  33.     void Awake()
  34.     {
  35.         if (SpawnShape == SpawnMode.staticPoint)
  36.         {
  37.             for (int i = 0; i < cloneCount; i++)
  38.             {
  39.                 Vector3 spawnPosition = setPosition;
  40.                 if (LookAtMode == LookAt.lookAtCentre)
  41.                 {
  42.                     Vector3 direction = (transform.position - setPosition);
  43.                     direction.y = 0;
  44.                     if (dontLookX)
  45.                     {
  46.                         direction.x = setRotation.x;
  47.                     }
  48.                     if (dontLookY)
  49.                     {
  50.                         direction.y = setRotation.y;
  51.                     }
  52.                     if (dontLookZ)
  53.                     {
  54.                         direction.z = setRotation.z;
  55.                     }
  56.                     Quaternion lookDirection = Quaternion.LookRotation(direction);
  57.                     spawnPosition += new Vector3(0, staticCircleY, 0);
  58.                     GameObject clone = Instantiate(arrayChildren[Random.Range(0, arrayChildren.Length)], transform.TransformPoint(spawnPosition), lookDirection);
  59.                     clone.transform.parent = transform;
  60.                 }
  61.                 if (LookAtMode == LookAt.lookAtSpawner)
  62.                 {
  63.                     Vector3 direction = (transform.position - setPosition);
  64.                     direction.y = 0;
  65.                     if (dontLookX)
  66.                     {
  67.                         direction.x = setRotation.x;
  68.                     }
  69.                     if (dontLookY)
  70.                     {
  71.                         direction.y = setRotation.y;
  72.                     }
  73.                     if (dontLookZ)
  74.                     {
  75.                         direction.z = setRotation.z;
  76.                     }
  77.                     Quaternion lookDirection = Quaternion.LookRotation(direction);
  78.                     GameObject clone = Instantiate(arrayChildren[Random.Range(0, arrayChildren.Length)], transform.TransformPoint(spawnPosition), lookDirection);
  79.                     clone.transform.parent = transform;
  80.                 }
  81.                 if (LookAtMode == LookAt.customRotation)
  82.                 {
  83.                     if(setRotation == Vector3.zero)
  84.                     {
  85.                         setRotation = Vector3.forward;
  86.                     }
  87.                     Quaternion lookDirection = Quaternion.LookRotation(setRotation);
  88.                     GameObject clone = Instantiate(arrayChildren[Random.Range(0,arrayChildren.Length)], transform.TransformPoint(spawnPosition), lookDirection);
  89.                     clone.transform.parent = transform;
  90.                 }
  91.             }
  92.         }
  93.         if(SpawnShape == SpawnMode.staticLine)
  94.         {
  95.             for (int i = 0; i < cloneCount; i++)
  96.             {
  97.                 Vector3 spawnPosition = setPosition * i;
  98.                 if (LookAtMode == LookAt.lookAtCentre)
  99.                 {
  100.                     Vector3 direction = (transform.position - setPosition);
  101.                     direction.y = 0;
  102.                     if (dontLookX)
  103.                     {
  104.                         direction.x = setRotation.x;
  105.                     }
  106.                     if (dontLookY)
  107.                     {
  108.                         direction.y = setRotation.y;
  109.                     }
  110.                     if (dontLookZ)
  111.                     {
  112.                         direction.z = setRotation.z;
  113.                     }
  114.                     Quaternion lookDirection = Quaternion.LookRotation(direction);
  115.                     spawnPosition += new Vector3(0, staticCircleY, 0);
  116.                     GameObject clone = Instantiate(arrayChildren[Random.Range(0, arrayChildren.Length)], transform.TransformPoint(spawnPosition), lookDirection);
  117.                     clone.transform.parent = transform;
  118.                 }
  119.                 if (LookAtMode == LookAt.lookAtSpawner)
  120.                 {
  121.                     Vector3 direction = (transform.position - setPosition);
  122.                     direction.y = 0;
  123.                     if (dontLookX)
  124.                     {
  125.                         direction.x = setRotation.x;
  126.                     }
  127.                     if (dontLookY)
  128.                     {
  129.                         direction.y = setRotation.y;
  130.                     }
  131.                     if (dontLookZ)
  132.                     {
  133.                         direction.z = setRotation.z;
  134.                     }
  135.                     Quaternion lookDirection = Quaternion.LookRotation(direction);
  136.                     GameObject clone = Instantiate(arrayChildren[Random.Range(0, arrayChildren.Length)], transform.TransformPoint(spawnPosition), lookDirection);
  137.                     clone.transform.parent = transform;
  138.                 }
  139.                 if (LookAtMode == LookAt.customRotation)
  140.                 {
  141.                     if (setRotation == Vector3.zero)
  142.                     {
  143.                         setRotation = Vector3.forward;
  144.                     }
  145.                     Quaternion lookDirection = Quaternion.LookRotation(setRotation);
  146.                     GameObject clone = Instantiate(arrayChildren[Random.Range(0, arrayChildren.Length)], transform.TransformPoint(spawnPosition), lookDirection);
  147.                     clone.transform.parent = transform;
  148.                 }
  149.             }
  150.         }
  151.         if (SpawnShape == SpawnMode.staticCircle)
  152.         {
  153.             for (int i = 0; i < cloneCount; i++)
  154.             {
  155.                 float theta = ((2 * Mathf.PI) / cloneCount) * i;
  156.                 float staticCircleX = Mathf.Cos(theta);
  157.                 float staticCircleZ = Mathf.Sin(theta);
  158.                 Vector3 positionAroundCircle = new Vector3(staticCircleX * spawnRadiusScale, 0, staticCircleZ * spawnRadiusScale);
  159.                 if (LookAtMode == LookAt.lookAtCentre)
  160.                 {
  161.                     Vector3 direction = transform.position - positionAroundCircle;
  162.                     if (dontLookX)
  163.                     {
  164.                         direction.x = setRotation.x;
  165.                     }
  166.                     if (dontLookY)
  167.                     {
  168.                         direction.y = setRotation.y;
  169.                     }
  170.                     if (dontLookZ)
  171.                     {
  172.                         direction.z = setRotation.z;
  173.                     }
  174.                     Quaternion lookDirection = Quaternion.LookRotation(direction);
  175.                     positionAroundCircle += new Vector3(0, staticCircleY, 0);
  176.                     GameObject clone = Instantiate(arrayChildren[Random.Range(0, arrayChildren.Length)], transform.TransformPoint(positionAroundCircle), lookDirection);
  177.                     clone.transform.parent = transform;
  178.                 }
  179.                 if (LookAtMode == LookAt.lookAtSpawner)
  180.                 {
  181.                     positionAroundCircle += new Vector3(0, staticCircleY, 0);
  182.                     Vector3 direction = transform.position - positionAroundCircle;
  183.                     if (dontLookX)
  184.                     {
  185.                         direction.x = setRotation.x;
  186.                     }
  187.                     if (dontLookY)
  188.                     {
  189.                         direction.y = setRotation.y;
  190.                     }
  191.                     if (dontLookZ)
  192.                     {
  193.                         direction.z = setRotation.z;
  194.                     }
  195.                     Quaternion lookDirection = Quaternion.LookRotation(direction);
  196.                     GameObject clone = Instantiate(arrayChildren[Random.Range(0, arrayChildren.Length)], transform.TransformPoint(positionAroundCircle), lookDirection);
  197.                     clone.transform.parent = transform;
  198.                 }
  199.                 if (LookAtMode == LookAt.customRotation)
  200.                 {
  201.                     if (setRotation == Vector3.zero)
  202.                     {
  203.                         setRotation = Vector3.forward;
  204.                     }
  205.                     Quaternion lookDirection = Quaternion.LookRotation(setRotation);
  206.                     GameObject clone = Instantiate(arrayChildren[Random.Range(0, arrayChildren.Length)], transform.TransformPoint(positionAroundCircle), lookDirection);
  207.                     clone.transform.parent = transform;
  208.                 }
  209.             }
  210.         }
  211.         if (SpawnShape == SpawnMode.randomCircle)
  212.         {
  213.             for (int i = 0; i < cloneCount; i++)
  214.             {
  215.                 Vector2 onUnitCircle = Random.insideUnitCircle.normalized;
  216.                 Vector3 randomPosition = new Vector3(onUnitCircle.x * spawnRadiusScale, 0, onUnitCircle.y * spawnRadiusScale);
  217.                 if (LookAtMode == LookAt.lookAtCentre)
  218.                 {
  219.                     Vector3 direction = transform.position - randomPosition;
  220.                     if (dontLookX)
  221.                     {
  222.                         direction.x = setRotation.x;
  223.                     }
  224.                     if (dontLookY)
  225.                     {
  226.                         direction.y = setRotation.y;
  227.                     }
  228.                     if (dontLookZ)
  229.                     {
  230.                         direction.z = setRotation.z;
  231.                     }
  232.                     Quaternion lookDirection = Quaternion.LookRotation(direction);
  233.                     randomPosition += new Vector3(0, staticCircleY, 0);
  234.                     GameObject clone = Instantiate(arrayChildren[Random.Range(0, arrayChildren.Length)], transform.TransformPoint(randomPosition), lookDirection);
  235.                     clone.transform.parent = transform;
  236.                 }
  237.                 if (LookAtMode == LookAt.lookAtSpawner)
  238.                 {
  239.                     randomPosition += new Vector3(0, staticCircleY, 0);
  240.                     Vector3 direction = transform.position - randomPosition;
  241.                     if (dontLookX)
  242.                     {
  243.                         direction.x = setRotation.x;
  244.                     }
  245.                     if (dontLookY)
  246.                     {
  247.                         direction.y = setRotation.y;
  248.                     }
  249.                     if (dontLookZ)
  250.                     {
  251.                         direction.z = setRotation.z;
  252.                     }
  253.                     Quaternion lookDirection = Quaternion.LookRotation(direction);
  254.                     GameObject clone = Instantiate(arrayChildren[Random.Range(0, arrayChildren.Length)], transform.TransformPoint(randomPosition), lookDirection);
  255.                     clone.transform.parent = transform;
  256.                 }
  257.                 if (LookAtMode == LookAt.customRotation)
  258.                 {
  259.                     if (randomRotMax == Vector3.zero)
  260.                     {
  261.                         randomRotMax = Vector3.one * 360;
  262.                     }
  263.                     if (setRotation == Vector3.zero)
  264.                     {
  265.                         setRotation = Vector3.forward;
  266.                     }
  267.                     Quaternion randomRotation = Quaternion.LookRotation(new Vector3(Random.Range(0, randomRotMax.x), Random.Range(0, randomRotMax.y), Random.Range(0, randomRotMax.z)));
  268.                     randomPosition += new Vector3(0, staticCircleY, 0);
  269.                     GameObject clone = Instantiate(arrayChildren[Random.Range(0, arrayChildren.Length)], transform.TransformPoint(randomPosition), randomRotation);
  270.                     clone.transform.parent = transform;
  271.                 }
  272.             }
  273.         }
  274.         if (SpawnShape == SpawnMode.randomSphere)
  275.         {
  276.             for (int i = 0; i < cloneCount; i++)
  277.             {
  278.                 Vector3 randomPosition = new Vector3(Random.insideUnitSphere.x * spawnRadiusScale, Random.insideUnitSphere.y * spawnRadiusScale, Random.insideUnitSphere.z * spawnRadiusScale);
  279.                 if (LookAtMode == LookAt.lookAtCentre)
  280.                 {
  281.                     Vector3 direction = transform.position - randomPosition;
  282.                     if (dontLookX)
  283.                     {
  284.                         direction.x = setRotation.x;
  285.                     }
  286.                     if (dontLookY)
  287.                     {
  288.                         direction.y = setRotation.y;
  289.                     }
  290.                     if (dontLookZ)
  291.                     {
  292.                         direction.z = setRotation.z;
  293.                     }
  294.                     Quaternion lookDirection = Quaternion.LookRotation(direction);
  295.                     randomPosition += new Vector3(0, staticCircleY, 0);
  296.                     GameObject clone = Instantiate(arrayChildren[Random.Range(0, arrayChildren.Length)], transform.TransformPoint(randomPosition), lookDirection);
  297.                     clone.transform.parent = transform;
  298.                 }
  299.                 if (LookAtMode == LookAt.lookAtSpawner)
  300.                 {
  301.                     randomPosition += new Vector3(0, staticCircleY, 0);
  302.                     Vector3 direction = transform.position - randomPosition;
  303.                     if (dontLookX)
  304.                     {
  305.                         direction.x = setRotation.x;
  306.                     }
  307.                     if (dontLookY)
  308.                     {
  309.                         direction.y = setRotation.y;
  310.                     }
  311.                     if (dontLookZ)
  312.                     {
  313.                         direction.z = setRotation.z;
  314.                     }
  315.                     Quaternion lookDirection = Quaternion.LookRotation(direction);
  316.                     GameObject clone = Instantiate(arrayChildren[Random.Range(0, arrayChildren.Length)], transform.TransformPoint(randomPosition), lookDirection);
  317.                     clone.transform.parent = transform;
  318.                 }
  319.                 if (LookAtMode == LookAt.customRotation)
  320.                 {
  321.                     if(randomRotMax == Vector3.zero)
  322.                     {
  323.                         randomRotMax = Vector3.one * 360;
  324.                     }
  325.                     if (setRotation == Vector3.zero)
  326.                     {
  327.                         setRotation = Vector3.forward;
  328.                     }
  329.                     Quaternion randomRotation = Quaternion.LookRotation(new Vector3(Random.Range(0, randomRotMax.x), Random.Range(0, randomRotMax.y), Random.Range(0, randomRotMax.z)));
  330.                     GameObject clone = Instantiate(arrayChildren[Random.Range(0, arrayChildren.Length)], transform.TransformPoint(randomPosition), randomRotation);
  331.                     clone.transform.parent = transform;
  332.                 }
  333.             }
  334.         }
  335.     }
  336. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement