【问题标题】:How can I change position of instantiate objects (clones)?如何更改实例化对象(克隆)的位置?
【发布时间】:2016-06-24 00:28:42
【问题描述】:

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

代码如下:

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

     }

提前谢谢你!

编辑: 正如我在评论中所说,我现在可以将我的球体放置在中间没有间隙。但是,一旦我为球体设置动画(使用正弦波),球体之间仍然存在间隙。我该如何适应这个?我应该将 Start 函数的代码复制到 Update 函数中吗?

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

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);  
}




}

【问题讨论】:

  • 这不是 C,你是说 C++ 吗?
  • 你是对的。代码是用 C# 编写的。我选择 C# 作为标签,但一旦我更新标签,它就会写成 C++。不知道为什么...
  • 如果您知道对象的大小,您可以简单地增加价值。例如你只想水平放置物体,在 Y up z front world 中你只能添加 initialposition + i*objectSize。
  • 我不明白你的意思,对不起。你能给我一个例子吗。我必须在脚本中的哪个位置进行更改?谢谢。

标签: c# unity3d initialization position scale


【解决方案1】:

我认为您可以制定 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 变量,根据您的需要进行调整。

您还可以使用plotPointObject.GetComponent&lt;MeshRenderer&gt;().bounds.size 获取对象距离,因此distanceFromEachOther 可以是,例如distanceFromEachOther = plotPointObject.GetComponent&lt;MeshRenderer&gt;().bounds.size.x + 5。因此,您将拥有彼此完美距离为 5 的对象。

【讨论】:

  • 非常感谢。它适用于您建议的第二种方法。现在的问题是,只要我在 void 更新函数中以正弦波对所有内容进行动画处理,差距仍然存在。是否必须在void update中复制start update的代码?
【解决方案2】:

试一试:

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);


}

【讨论】:

  • 我认为我应该在开始更新功能中执行此操作?为什么我在关闭更新功能时可以更改球体的外观,以便我的所有球体都对齐。但是,一旦我用所有球体为正弦波设置动画,球体之间就会再次出现那个间隙。
猜你喜欢
  • 2020-10-14
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 2015-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-18
相关资源
最近更新 更多