-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaceObjectsOnLine.cs
More file actions
106 lines (82 loc) · 3.24 KB
/
placeObjectsOnLine.cs
File metadata and controls
106 lines (82 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class placeObjectsOnLine : MonoBehaviour
{
public GameObject objectToPlace; // The GameObject to place along the line
public LineRenderer lineRenderer; // Reference to the LineRenderer component
[Header("Use Context Menu to Place Objects.")]
[Min(1)]
public int numberOfObjects = 10; // Number of objects to place along the line
public bool PlaceObject = false;
public bool SendToRecycleBin = false;
private void OnValidate()
{
if (SendToRecycleBin)
{
OrphanAllChildren();
SendToRecycleBin = false;
}
if (PlaceObject)
{
PlaceObjects();
PlaceObject = false;
}
}
[ContextMenu("PlaceObjects")]
void PlaceObjects()
{
if (objectToPlace == null || lineRenderer == null)
{
Debug.LogError("Object to place or LineRenderer not assigned!");
return;
}
float totalDistance = 0f;
// Calculate the total distance of the line
for (int i = 0; i < lineRenderer.positionCount - 1; i++)
{
totalDistance += Vector3.Distance(lineRenderer.GetPosition(i), lineRenderer.GetPosition(i + 1));
}
float step = totalDistance / (numberOfObjects - 1);
float currentDistance = 0f;
for (int i = 0; i < numberOfObjects -1; i++)
{
Vector3 position = GetPositionAtDistance(currentDistance);
GameObject obj = Instantiate(objectToPlace, position + transform.position, Quaternion.identity);
obj.transform.parent = transform;
currentDistance += step;
}
Vector3 lastPosition = lineRenderer.GetPosition(lineRenderer.positionCount - 1);
GameObject obj2 = Instantiate(objectToPlace, lastPosition + transform.position, Quaternion.identity);
obj2.transform.parent = transform;
}
// Helper method to get the position at a specific distance along the line
Vector3 GetPositionAtDistance(float distance)
{
float currentDistance = 0f;
for (int i = 0; i < lineRenderer.positionCount - 1; i++)
{
float segmentDistance = Vector3.Distance(lineRenderer.GetPosition(i), lineRenderer.GetPosition(i + 1));
if (currentDistance + segmentDistance >= distance)
{
float t = (distance - currentDistance) / segmentDistance;
return Vector3.Lerp(lineRenderer.GetPosition(i), lineRenderer.GetPosition(i + 1), t);
}
currentDistance += segmentDistance;
}
// This should not happen if the distance is within the total line distance
return Vector3.zero;
}
[ContextMenu("Recycle Bin")]
private void OrphanAllChildren()
{
int childCount = transform.childCount;
var rubbishbin = new GameObject();
rubbishbin.name = "Recycle Bin";
for (int i = 0; i < childCount; i++)
{
transform.GetChild(0).parent = rubbishbin.transform;
}
rubbishbin.gameObject.SetActive(false);
}
}