如何更改实例化对象(克隆)的位置?

时间:2016-03-09 14:49:36

标签: c# unity3d initialization position scale

我在游戏中创建了一系列球体克隆。之后,我调整了比例,使它们看起来更小。但是,现在这些领域之间存在差距......我将不得不改变这个实例游戏对象的位置。我已经完全改变了我的代码,但没有任何反应。所以,我需要你的帮助!我怎样才能做到这一点?我会有非常小的球体,它们位于一起。

这里是代码:

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

     }

提前谢谢你!

编辑: 正如我在评论中所说,我现在已经实现了在两者之间没有间隙的情况。然而,一旦我为我的球体(用正弦波)制作动画,球体之间仍然存在间隙。我怎样才能适应这个?我应该在更新功能中复制启动功能的代码吗?

我很乐意得到一些帮助。非常感谢你!

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 个答案:

答案 0 :(得分:0)

我认为你可以制作Barış解决方案。 对于要实例化的每个新对象,您将其位置设置为持续实例化位置,添加对象本身的大小,或者您希望彼此具有的任何距离。

var initialPosition = 0;
var distanceFromEachOther = 20;
for (int i = 0; i < numberOfPoints; i++) {
    var newPos = new Vector3(initialPosition + (i * distanceFromEachOther), 0, 0);
    plotPoints[i] = (GameObject)GameObject.Instantiate(plotPointObject, newPos, Quaternion.identity);
}

这会在X枢轴上的球体之间产生差距,具体取决于它们的大小。根据您的需要更改distanceFromEachOther var。

您还可以使用plotPointObject.GetComponent<MeshRenderer>().bounds.size获取对象距离,因此distanceFromEachOther可以是distanceFromEachOther = plotPointObject.GetComponent<MeshRenderer>().bounds.size.x + 5。那么你将拥有彼此完全距离为5的物体。

答案 1 :(得分:0)

尝试一下:

Transform objectToSpawn;
for (int i = 0; i < numberOfPoints; i++)
{
    float someX = 200;
    float someY = 200;
    Transform t = Instantiate(objectToSpawn, new Vector3(i -(numberOfPoints/2), 0, 0), Quaternion.identity) as Transform;
    plotPoints[i] = t.gameObject;
    t.position = new Vector(someX, someY);


}