我在遊戲中創建了一系列球體克隆。之後,我調整了尺度,使它們看起來更小。然而,現在這些領域之間存在差距......我必須改變這個不穩定的遊戲對象的位置。我已經完全改變了我的代碼,但沒有任何反應。所以請我需要你的幫助!我怎樣才能做到這一點?我會有很小的球體,它們靠近在一起。如何更改實例化對象(克隆)的位置?
下面的代碼:
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
}
謝謝你已經提前!
編輯: 正如我在現在所取得的評論中所說,我的球體之間沒有間隙。然而,只要我使用正弦波對球體進行動畫處理,球體之間仍然存在這種差距。我該如何調整呢?我應該在Update函數中複製Start函數的代碼嗎?
我會很高興得到一些幫助。非常感謝你!
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++編寫的。不知道爲什麼... – sportente
如果你知道對象的大小,你可以簡單地增加值。例如,您只想將對象水平放置在Y向上,前向世界中只能添加初始位置+ i * objectSize。 –