How can I change position of instantiate objects (clones)?

Viewed 1118

I created a series of sphere clones in my game. After that I adapted the scale so that they appear smaller. However, now there is a gap between these spheres ... and I would have to change the position of this instatiate game objects. I changed my code already exactly at this position but nothing happens. So please I need your help! How can I do this? I would have very small spheres which are located near together.

Here the code:

using UnityEngine;  
using System.Collections;    

public class SineWave : MonoBehaviour {


     private GameObject plotPointObject;
     private int numberOfPoints= 100;
     private float animSpeed =1.0f;
     private float scaleInputRange = 8*Mathf.PI; // scale number from [0 to 99] to [0 to 2Pi] //Zahl vor Mathf, Anzahl Bön 
     private float scaleResult = 2.5f; // Y Achse Range 
     public bool animate = true;




     GameObject[] plotPoints;


     // Use this for initialization
     void Start () {

         if (plotPointObject == null) //if user did not fill in a game object to use for the plot points
             plotPointObject = GameObject.CreatePrimitive(PrimitiveType.Sphere); //create a sphere


         //add Material to the spheres , load material in the folder Resources/Materials 
         Material myMaterial = Resources.Load("Materials/green", typeof(Material)) as Material;
         plotPointObject.GetComponent<MeshRenderer> ().material = myMaterial;


         //change the scale of the spheres 
         //plotPointObject.transform.localScale = Vector3.one * 0.5f ;
         plotPointObject.transform.localScale -= new Vector3(0.5f,0.5f,0.5f);


         plotPoints = new GameObject[numberOfPoints]; //creat an array of 100 points.
         //plotPointObject.GetComponent<MeshRenderer> ().material =Material.Load("blue") as Material


         //plotPointObject.transform.localScale -= new Vector3 (0.5F, 0.5F, 0.5F); //neu: change the scale of the spheres


         for (int i = 0; i < numberOfPoints; i++)
         {
             plotPoints[i] = (GameObject)GameObject.Instantiate(plotPointObject, new Vector3(i -
(numberOfPoints/2), 0, 0), Quaternion.identity); //this specifies
what object to create, where to place it and how to orient it

         }
         //we now have an array of 100 points- your should see them in the hierarchy when you hit play
         plotPointObject.SetActive(false); //hide the original

     }

Thank you already in advance!

Edit: As I said in the comment I achieved now to place my spheres without a gap in between. However, as soon as I animate my spheres (with a sine wave) there is still that gap between the spheres. How can I adapt this? Should I copy the code of the Start function in the Update function?

I would be very happy to get some help. Thank you very much!

enter code here void Update()
{
    for (int i = 0; i < numberOfPoints; i++)
    {
        float functionXvalue = i * scaleInputRange / numberOfPoints; // scale number from [0 to 99] to [0 to 2Pi]
        if (animate)
        {
            functionXvalue += Time.time * animSpeed;

        }




        plotPoints[i].transform.position = new Vector3(i - (numberOfPoints/2), ComputeFunction(functionXvalue) * scaleResult, 0); 

        //print (plotPointObject.GetComponent<MeshRenderer> ().bounds.size.x);


        // put the position information of sphere clone 50 in a vector3 named posSphere 
        posSphere = plotPoints [50].transform.position;


    }

    //print position of sphere 50 in console 
    //print (posSphere);
}


float ComputeFunction(float x)
{
    return Mathf.Sin(x);  
}




}
2 Answers
Related