2
\$\begingroup\$

I am using the editor code below to create copies of a game object in a circle with a given radius from the cloned object. The result is A in the image below, the cloned object at the centre with copies around. I want to achieve B, which has the game object (CircleSpawn) at the centre and the cloned object included in the copies that are created around the centre. The cloned object should be able to rotate and function in a way similar to it's copies. How can I fix my code to create this?

enter image description here

CircleSpawn

public class CircleSpawn : MonoBehaviour
{

public float radius, radiusLast, spin, spinLast;
public int numOfItems;
public GameObject clonedObject;
public List<GameObject> spawnedObjects;


}

CircleSpawnEditor

[CustomEditor(typeof(CircleSpawn))]
public class CircleSpawnEditor : Editor
{

public override void OnInspectorGUI()
{
    var tar = (CircleSpawn)target;
    tar.clonedObject = (GameObject)EditorGUILayout.ObjectField(tar.clonedObject,
        typeof(GameObject), true);
    if (!tar.clonedObject) return;


    EditorGUILayout.LabelField("Radius"); // Set as required
    tar.radius = EditorGUILayout.Slider(tar.radius, 0f, 5f);
    EditorGUILayout.LabelField("Angle"); // Set as required
    tar.spin = EditorGUILayout.Slider(tar.spin, 0f, 360f);
    EditorGUILayout.LabelField("Number of Items"); // Set as required
    tar.numOfItems = EditorGUILayout.IntSlider(tar.numOfItems, 0, 12);
    EditorGUILayout.LabelField("Object");

    float angle, angleBetween = 360.0f / tar.numOfItems;

    if (tar.spawnedObjects == null)
        tar.spawnedObjects = new List<GameObject>();


    if (tar.spawnedObjects.Count != tar.numOfItems)
    {
        foreach (var ob in tar.spawnedObjects)
            DestroyImmediate(ob);

        tar.spawnedObjects.Clear();
        angle = 0f;

        for (int i = 0; i < tar.numOfItems; i++)
        {
            var rot = Quaternion.Euler(0f, 0f, tar.spin + angle);
            var localPos = rot * Vector3.right * tar.radius;
            tar.spawnedObjects.Add(Instantiate(tar.clonedObject,
            tar.transform.position + localPos, rot));
            angle += angleBetween;


        }
    }


    if (!Mathf.Approximately(tar.spin, tar.spinLast) ||
        !Mathf.Approximately(tar.radius, tar.radiusLast))
    {
        tar.spinLast = tar.spin;
        tar.radiusLast = tar.radius;
        angle = 0f;

        for (int i = 0; i < tar.numOfItems; i++)
        {
            var rot = Quaternion.Euler(0f, 0f, tar.spin + angle);
            var localPos = rot * Vector3.right * tar.radius;
            tar.spawnedObjects[i].transform.position =
            tar.transform.position + localPos;
            tar.spawnedObjects[i].transform.rotation = rot;
            angle += angleBetween;

        }
    }
}
}
\$\endgroup\$
1
\$\begingroup\$
  1. wow thanks for the code very interesting
  2. As I understand it, you get version A because you add an object from the scene to the "clonedObject" field. You need to create a prefab of the target object and in the "clonedObject" field specify the prefab itself enter image description here
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.