How can I change position of instantiate objects (clones)?
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
}
And here a screenshot of how it looks like for the moment:
Answer by richardgengle · Sep 18, 2020 at 06:56 PM
@sportente heres what i am using... and its kinda klunky, in that simulation speed is poor for many particle/clone water simulation...remember to ASSIGN THE RIGIDBODY TO YOUR OBJECT TO CLONE, and also to DRAG IT TO THE PLACE ON THE SCRIPT WHERE THE "PUBLIC" SHOWS UP.
==== // before start routine
public Rigidbody2D tearrb;
float x=0;// pick values of x, dx, y, dy to whatever you need
float y=0;
float dx=1;
float dy=1;// pick values of x, dx, y, dy to whatever you need
// in update routine
Rigidbody2D clone;
x=4; // pick values of x, dx, y, dy to whatever you need
y=4;
dx=1; // pick values of x, dx, y, dy to whatever you need
dy=1;
clone2 = Instantiate(tearrb, new Vector2(x, y), tearrb.transform.rotation);
clone2.position += new Vector2(dx, dy);
Destroy(clone2, 10f); // remove this line if u dont want to destroy/remove your clone
=== tell me if u can get something working ;]