2016-03-09 39 views
3

我在遊戲中創建了一系列球體克隆。之後,我調整了尺度,使它們看起來更小。然而,現在這些領域之間存在差距......我必須改變這個不穩定的遊戲對象的位置。我已經完全改變了我的代碼,但沒有任何反應。所以請我需要你的幫助!我怎樣才能做到這一點?我會有很小的球體,它們靠近在一起。如何更改實例化對象(克隆)的位置?

下面的代碼:

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




} 
+0

這不是C,你的意思是C++? –

+0

你說得對。代碼是用C#編寫的。我選擇了C#作爲標籤,但只要我更新了標籤,它就是用C++編寫的。不知道爲什麼... – sportente

+0

如果你知道對象的大小,你可以簡單地增加值。例如,您只想將對象水平放置在Y向上,前向世界中只能添加初始位置+ i * objectSize。 –

回答

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變量。

您還可以通過plotPointObject.GetComponent<MeshRenderer>().bounds.size獲得物距,因此distanceFromEachOther可以是,例如distanceFromEachOther = plotPointObject.GetComponent<MeshRenderer>().bounds.size.x + 5。那麼你將擁有距彼此完美距離5的物體。

+0

非常感謝。它與您建議的第二種方法一起工作。現在的問題是,只要我在無效更新函數中以正弦波動畫所有動畫,差距仍然存在。我必須在無效更新中複製啓動更新的代碼嗎? – sportente

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


} 
+0

我想我應該在開始更新功能中做到這一點?當我關閉更新功能時,爲什麼我可以更改球體的外觀,以便我所有的球體都對齊。但是,只要我用所有球體對我的正弦波進行動畫處理,球體之間就會再次出現間隙。 – sportente