Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // PropLinePlacer - a tool for placing lines of prefabs
- // By Jon Manning
- // MIT License
- // Copyright (c) 2019 Secret Lab Pty Ltd
- // Permission is hereby granted, free of charge, to any person obtaining a
- // copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- // The above copyright notice and this permission notice shall be included
- // in all copies or substantial portions of the Software.
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
- // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
- // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
- // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
- // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
- // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- using UnityEngine;
- public class PropLinePlacer : MonoBehaviour
- {
- /// <summary>
- /// The end point of the line, in local space.
- /// </summary>
- /// <remarks>The start point is the transform of this object.</remarks>
- public Vector3 endPoint = new Vector3(1, 0, 0);
- /// <summary>
- /// The possible prefabs that this line should contain. Prefabs will be
- /// randomly selected from this list.
- /// </summary>
- public GameObject[] prefabs;
- /// <summary>
- /// The number of objects that should be created.
- /// </summary>
- public uint numberOfObjects = 3;
- /// <summary>
- /// If true, the created objects will be randomly rotated around the Y
- /// axis.
- /// </summary>
- public bool randomizeYRotation = true;
- /// <summary>
- /// The amount of distance, in the X and Z directions, to randomly
- /// offset our objects.
- /// </summary>
- public float randomXZDistance = 0.05f;
- /// <summary>
- /// The degree of distance along the line to shift our objects.
- /// </summary>
- public float maxLineDisplacement = 0f;
- /// <summary>
- /// A list of materials to randomly assign to the created objects.
- /// </summary>
- /// <remarks>If this array is empty, the created objects will not have
- /// their materials changed.</remarks>
- public Material[] materials;
- /// <summary>
- /// Produces an array of positions, in world space, for objects to be
- /// created at.
- /// </summary>
- /// <returns>The positions, in world space, for objects to be
- /// placed.</returns>
- public Vector3[] GetSpawnPositions()
- {
- // Early out if we're producing zero objects.
- if (numberOfObjects == 0)
- {
- return new Vector3[0];
- }
- var result = new Vector3[numberOfObjects];
- // Save the state of the random number generator, because we're
- // about to blow it away
- var state = UnityEngine.Random.state;
- // Reset the random number generator so that it's consistent
- // between frames
- Random.InitState(0);
- // For each object we want to create, calculate its position
- for (int i = 0; i < numberOfObjects; i++)
- {
- // Figure out how far along the line we are, from 0 to 1
- var t = i / (float)(numberOfObjects - 1);
- // Randomly displace this along the line (this is why we're
- // preserving random state, otherwise the positions would
- // change every frame that the object is selected in the
- // editor)
- t += Random.Range(-maxLineDisplacement, maxLineDisplacement) * 1f / numberOfObjects;
- // Calculate this position along the line!
- var position = Vector3.Lerp(
- transform.position, // start point
- endPoint + transform.position, // end point
- t // position on the line (from 0 to 1)
- );
- result[i] = position;
- }
- // Restore the random state we saved earlier
- Random.state = state;
- // Return the list of positions!
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement