You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
853 B

5 years ago
  1. using UnityEngine;
  2. public class SplineDecorator : MonoBehaviour {
  3. public BezierSpline spline;
  4. public int frequency;
  5. public bool lookForward;
  6. public Transform[] items;
  7. private void Awake () {
  8. if (frequency <= 0 || items == null || items.Length == 0) {
  9. return;
  10. }
  11. float stepSize = frequency * items.Length;
  12. if (spline.Loop || stepSize == 1) {
  13. stepSize = 1f / stepSize;
  14. }
  15. else {
  16. stepSize = 1f / (stepSize - 1);
  17. }
  18. for (int p = 0, f = 0; f < frequency; f++) {
  19. for (int i = 0; i < items.Length; i++, p++) {
  20. Transform item = Instantiate(items[i]) as Transform;
  21. Vector3 position = spline.GetPoint(p * stepSize);
  22. item.transform.localPosition = position;
  23. if (lookForward) {
  24. item.transform.LookAt(position + spline.GetDirection(p * stepSize));
  25. }
  26. item.transform.parent = transform;
  27. }
  28. }
  29. }
  30. }