1
\$\begingroup\$

This will instantiate the prefab 10 times at the same position. And I want to add equal gap between the objects.

The prefabs should be instantiate on the drawn circle. I want inside this:

if (moveInCircles) To move in circles all the new prefabs.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class rotate : MonoBehaviour
{
    [Header("Spin")]
    public bool spin = true;
    public Vector3 Direction;
    [Range(0, 300)]
    public float speed = 10f;
    public bool randomSpeed = false;
    public bool randomDirection = false;
    [Range(0f, 100f)]
    public float timeDirChange;
    public Vector3 defaultDirection = new Vector3(1, 0, 0);

    [Space(5)]

    [Header("Move in circles")]
    public bool moveInCircles = false;
    public GameObject rotateAroundTarget;
    public Vector3 axis;//by which axis it will rotate. x,y or z.
    public float rotationSpeed; //or the speed of rotation.
    public float upperLimit, lowerLimit, delay;// upperLimit & lowerLimit: heighest & lowest height;
    [Range(5, 50)]
    public float radius = 5;
    private float height, prevHeight, time;//height:height it is trying to reach(randomly generated); prevHeight:stores last value of height;delay in radomness; 


    private float nextRotationTime = 0f;
    private int counter = 0;

    public DrawCircle dc;
    public GameObject prefab;
    public int numOfObjects;
    public int gap = 3;

    // Use this for initialization
    void Start()
    {
        dc.xradius = radius;

        if (prefab != null)
        {
            for (int i = 0; i < numOfObjects; i++)
            {


                Instantiate(prefab);

                var v = Quaternion.AngleAxis(Time.time * speed, Vector3.up) * new Vector3(radius, 0, 0);
                //prefab.transform.position = rotateAroundTarget.transform.position + v;
                prefab.transform.position = rotateAroundTarget.transform.position + v + new Vector3(rotateAroundTarget.transform.position.x * gap,0, 0);
            }
        }
    }

    private void Update()
    {
        if (randomSpeed)
        {
            speed = UnityEngine.Random.Range(0, 300);
        }

        if (spin)
        {
            if (randomDirection == false)
            {
                nextRotationTime = 0;
                timeDirChange = 0;
                Direction = defaultDirection;
            }
            else
            {
                if (Time.time > nextRotationTime)
                {
                    nextRotationTime += timeDirChange;
                    RandomDirection();
                }
            }

            transform.Rotate(Direction, speed * Time.deltaTime);
        }
        else
        {
            timeDirChange = 0;
            randomDirection = false;
            randomSpeed = false;
        }

        if (moveInCircles)
        {
            var v = Quaternion.AngleAxis(Time.time * speed, Vector3.up) * new Vector3(radius, 0, 0);
            transform.position = rotateAroundTarget.transform.position + v;
        }

        dc.xradius = radius;
    }

    private void RandomDirection()
    {
        Direction = new Vector3(UnityEngine.Random.Range(-1, 1), UnityEngine.Random.Range(-1, 1), UnityEngine.Random.Range(-1, 1));
        while (Direction == new Vector3(0, 0, 0))
        {
            counter++;
            Direction = new Vector3(UnityEngine.Random.Range(-1, 1), UnityEngine.Random.Range(-1, 1), UnityEngine.Random.Range(-1, 1));

            if (counter == 2)
            {
                Direction = new Vector3(1, 0, 0);
                break;
            }
        }

        counter = 0;
    }
}

This is the script that I draw the circle:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

[RequireComponent(typeof(LineRenderer))]
public class DrawCircle : MonoBehaviour
{
    [Range(0, 50)]
    public int segments = 50;
    [Range(0, 50)]
    public float xradius = 0;
    [Range(0, 50)]
    public float yradius = 0;
    LineRenderer line;
    public GameObject objectToDrawCircle;

    void Start()
    {
        line = objectToDrawCircle.GetComponent<LineRenderer>();
        line.positionCount = segments + 1;
        line.useWorldSpace = false;
        line.startWidth = 0.05f;
        line.endWidth = 0.05f;
        CreatePoints();
    }

    void Update()
    {
        if (xradius > 0)
        {
            yradius = xradius;
            CreatePoints();
        }
    }

    void CreatePoints()
    {
        float x;
        float y;
        float z;

        float angle = 20f;

        for (int i = 0; i < (segments + 1); i++)
        {
            x = Mathf.Sin(Mathf.Deg2Rad * angle) * xradius;
            z = Mathf.Cos(Mathf.Deg2Rad * angle) * yradius;

            line.SetPosition(i, new Vector3(x, 0, z));

            angle += (360f / segments + 1);
        }
    }
}

What I want is in the first script rotate in the Start to place prefabs on the drawn circle.

In the screenshot example. The circle I draw is in red and the prefabs are in green. I want to Instantiate the prefabs on the circle like the greens in the screenshot. But in equal gap between them.

circle with prefabs on it

\$\endgroup\$
1
\$\begingroup\$

create one empty gameobject in scene. create one prefab and save it in project folder. create one script and past below code in your script then attach this script to the empty object in unity editor.

public GameObject prefab;
int numOfObjects = 10;
// Use this for initialization
void Start () 
{
    if (prefab != null)
    {
        for (int i = 0; i < numOfObjects; i++)
        {  
            Instantiate(prefab,new Vector3(0.0f,i+1,0.0f),Quaternion.identity);
        }
    }
    else 
        Debug.Log("prefab not set in inspector");
}

after attaching the script reference the prefab in the script. then run the program.!

\$\endgroup\$
  • \$\begingroup\$ It's working but I forgot to edit my question with the full script. What I want is to Instantiate the new prefabs on the circle drawn circle with equal gaps. That's my problem. Sorry for that. \$\endgroup\$ – Daniel Lip Apr 26 '18 at 6:58
  • 1
    \$\begingroup\$ i didn't understood what you said. can you elaborate your question in detail? \$\endgroup\$ – Pravin Budharap Apr 26 '18 at 7:38
  • \$\begingroup\$ I updated my question again with the DrawCircle script that I'm using in the rotate script. What I want is in the first script rotate in the Start to place prefabs on the drawn circle. Not to move them just to Instantiate 10 new prefabs with equal gaps on the drawn circle. \$\endgroup\$ – Daniel Lip Apr 26 '18 at 8:36

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.