SHARE
TWEET

Untitled

a guest May 19th, 2019 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // PropLinePlacer - a tool for placing lines of prefabs
  2. // By Jon Manning
  3.  
  4. // MIT License
  5.  
  6. // Copyright (c) 2019 Secret Lab Pty Ltd
  7.  
  8. // Permission is hereby granted, free of charge, to any person obtaining a
  9. // copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15.  
  16. // The above copyright notice and this permission notice shall be included
  17. // in all copies or substantial portions of the Software.
  18.  
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  20. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  22. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  24. // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  25. // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26.  
  27. using UnityEngine;
  28.  
  29. public class PropLinePlacer : MonoBehaviour
  30. {
  31.     /// <summary>
  32.     /// The end point of the line, in local space.
  33.     /// </summary>
  34.     /// <remarks>The start point is the transform of this object.</remarks>
  35.     public Vector3 endPoint = new Vector3(1, 0, 0);
  36.  
  37.     /// <summary>
  38.     /// The possible prefabs that this line should contain. Prefabs will be
  39.     /// randomly selected from this list.
  40.     /// </summary>
  41.     public GameObject[] prefabs;
  42.  
  43.     /// <summary>
  44.     /// The number of objects that should be created.
  45.     /// </summary>
  46.     public uint numberOfObjects = 3;
  47.  
  48.     /// <summary>
  49.     /// If true, the created objects will be randomly rotated around the Y
  50.     /// axis.
  51.     /// </summary>
  52.     public bool randomizeYRotation = true;
  53.  
  54.     /// <summary>
  55.     /// The amount of distance, in the X and Z directions, to randomly
  56.     /// offset our objects.
  57.     /// </summary>
  58.     public float randomXZDistance = 0.05f;
  59.  
  60.     /// <summary>
  61.     /// The degree of distance along the line to shift our objects.
  62.     /// </summary>
  63.     public float maxLineDisplacement = 0f;
  64.  
  65.     /// <summary>
  66.     /// A list of materials to randomly assign to the created objects.
  67.     /// </summary>
  68.     /// <remarks>If this array is empty, the created objects will not have
  69.     /// their materials changed.</remarks>
  70.     public Material[] materials;
  71.  
  72.     /// <summary>
  73.     /// Produces an array of positions, in world space, for objects to be
  74.     /// created at.
  75.     /// </summary>
  76.     /// <returns>The positions, in world space, for objects to be
  77.     /// placed.</returns>
  78.     public Vector3[] GetSpawnPositions()
  79.     {
  80.         // Early out if we're producing zero objects.
  81.         if (numberOfObjects == 0)
  82.         {
  83.             return new Vector3[0];
  84.         }
  85.  
  86.         var result = new Vector3[numberOfObjects];
  87.  
  88.         // Save the state of the random number generator, because we're
  89.         // about to blow it away
  90.         var state = UnityEngine.Random.state;
  91.  
  92.         // Reset the random number generator so that it's consistent
  93.         // between frames
  94.         Random.InitState(0);
  95.  
  96.         // For each object we want to create, calculate its position
  97.         for (int i = 0; i < numberOfObjects; i++)
  98.         {
  99.             // Figure out how far along the line we are, from 0 to 1
  100.             var t = i / (float)(numberOfObjects - 1);
  101.  
  102.             // Randomly displace this along the line (this is why we're
  103.             // preserving random state, otherwise the positions would
  104.             // change every frame that the object is selected in the
  105.             // editor)
  106.             t += Random.Range(-maxLineDisplacement, maxLineDisplacement) * 1f / numberOfObjects;
  107.  
  108.             // Calculate this position along the line!
  109.             var position = Vector3.Lerp(
  110.                 transform.position, // start point
  111.                 endPoint + transform.position, // end point
  112.                 t // position on the line (from 0 to 1)
  113.             );
  114.  
  115.             result[i] = position;
  116.         }
  117.  
  118.         // Restore the random state we saved earlier
  119.         Random.state = state;
  120.  
  121.         // Return the list of positions!
  122.         return result;
  123.     }
  124. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top