Код: Выделить всё
[Header("Add Objects on Circle")]
public GameObject objectPrefab;
[Range(0, 50)]
public int numberOfObjects = 5;
[Range(0.1f, 5f)]
public float objectScale = 0.1f;
public bool addObjects = true;
private List spawnedObjects = new List();
private float previousBothRadius = -1;
Код: Выделить всё
private void OnValidate()
{
// Check if changeBothRadius was updated
if (changeBothRadius != previousBothRadius)
{
SyncRadii();
previousBothRadius = changeBothRadius;
}
if (line != null && draw)
{
line.enabled = true;
ConfigureLineRenderer();
CreatePoints();
}
else if (line != null)
{
line.enabled = false;
}
if (addObjects)
{
CreateOrUpdateAroundPoint(numberOfObjects, transform.position, xradius);
}
}
Код: Выделить всё
private void CreateOrUpdateAroundPoint(int num, Vector3 point, float radius)
{
if (num < 1) return; // Ensure at least one object is created
// Ensure the spawnedObjects list exists
if (spawnedObjects == null)
{
spawnedObjects = new List();
}
// If there are fewer objects than needed, create new ones
while (spawnedObjects.Count < num)
{
var obj = Instantiate(objectPrefab, Vector3.zero, Quaternion.identity);
obj.tag = "ObjectonCircle";
obj.name = "Object on Circle";
obj.transform.parent = transform; // Optional: make them children of this object
spawnedObjects.Add(obj);
}
// If there are more objects than needed, destroy the extras
while (spawnedObjects.Count > num)
{
var obj = spawnedObjects[spawnedObjects.Count - 1];
spawnedObjects.RemoveAt(spawnedObjects.Count - 1);
if (Application.isPlaying)
{
Destroy(obj);
}
else
{
#if UNITY_EDITOR
// Defer destruction to avoid DestroyImmediate restrictions
EditorApplication.delayCall += () =>
{
if (obj != null) DestroyImmediate(obj);
};
#endif
}
}
// Update positions of all objects
float angleStep = 360f / num; // Equal angle step for even distribution
for (int i = 0; i < num; i++)
{
/* Calculate angle in radians */
var angle = angleStep * i * Mathf.Deg2Rad;
/* Get the vector direction */
var vertical = Mathf.Sin(angle);
var horizontal = Mathf.Cos(angle);
var spawnDir = new Vector3(horizontal, 0, vertical);
/* Get the spawn position */
var spawnPos = point + spawnDir * radius;
/* Update object position */
if (spawnedObjects[i] != null) // Ensure the object exists before updating
{
spawnedObjects[i].transform.position = spawnPos;
spawnedObjects[i].transform.localScale = Vector3.one * objectScale;
/* Rotate the object to face the center */
spawnedObjects[i].transform.LookAt(point);
}
}
}
Я предполагаю, что слишком быстрое перемещение мыши не дает достаточно времени для уничтожения всех объектов.
Есть ли способ это исправить или сделать так, чтобы объекты уничтожались в зависимости от значения, независимо от скорости мыши?
Подробнее здесь: https://stackoverflow.com/questions/792 ... e-the-acti