2014-01-21 41 views
1

標題幾乎總結了它。我的敵人預製裝置只能在Y軸上實例化,但硬幣預製裝置正在被實例化,即使它們具有相同的座標也是如何。預製沒有在給定的位置實例化

import System.Collections.Generic; 

var enemyPrefab : GameObject; 
var coinPrefab : GameObject; 
public var Coinnumber = 1; 
public var Coinnumberside : float = 0.0f; 

    function OnTriggerEnter(other: Collider) 
    { 
    if (other.tag == "Trigger") 
    {  
     var StagePosition : Vector3; 
     StagePosition.z = 5.69 + Coinnumber; 
     StagePosition.x = 0.0 + Coinnumberside; 
     StagePosition.y = 0.4; 

     Instantiate(coinPrefab, StagePosition, Quaternion.identity); 

    Coinnumber = Coinnumber + Random.Range(5,10); 
    Coinnumberside = Coinnumberside + Random.Range(0.2,-0.4); 



     var EnemyPosition : Vector3; 
     EnemyPosition.z = 5.69 + Coinnumber; 
     EnemyPosition.x = 0.0 + Coinnumberside; 
     EnemyPosition.y = 0.4; 

     Instantiate(enemyPrefab, EnemyPosition, Quaternion.identity); 

    Coinnumber = Coinnumber + Random.Range(5,10); 
    Coinnumberside = Coinnumberside + Random.Range(0.2,-0.4); 




    } 
} 
+1

你的問題有問題......他們沒有相同的座標。 x和z軸依賴於在兩個賦值之間改變的變量,這是偶然的。也許Coinnumber和Coinnumberside只能在函數結束時更改? –

+0

除了別人已經指出,你怎麼反對樞紐點,全球和本地。他們是否在正確的目標點和方向? – VicM

回答

0
import System.Collections.Generic; 

var enemyPrefab : GameObject; 
var coinPrefab : GameObject; 
public var Coinnumber = 1; 
public var Coinnumberside : float = 0.0f; 
public var Enemynumber = 1; 
public var Enemynumberside : float = 0.0f; 

function OnTriggerEnter(other: Collider) 
{ 
    if (other.tag == "Trigger") 
    {  
     var StagePosition : Vector3; 
     StagePosition.z = 5.69 + Coinnumber; 
     StagePosition.x = 0.0 + Coinnumberside; 
     StagePosition.y = 0.4; 

     Instantiate(coinPrefab, StagePosition, Quaternion.identity); 

     Coinnumber = Coinnumber + Random.Range(5,10); 
     Coinnumberside = Coinnumberside + Random.Range(0.2,-0.4); 
    } 

    if(other.tag == "Trigger") 
    { 
     var EnemyPosition : Vector3; 
     EnemyPosition.z = 5.69 + Enemynumber; 
     EnemyPosition.x = 0.0 + Enemynumberside; 
     EnemyPosition.y = 0.4; 

     Instantiate(enemyPrefab, EnemyPosition, Quaternion.identity); 

     Enemynumber = Enemynumber + Random.Range(5,10); 
     Enemynumberside = Enemynumberside + Random.Range(0.2,-0.4); 

    } 
} 

我還不是很確定爲什麼這個工作後,我分離他們,但它沒有,所以我把它作爲一個答案。感謝所有回答並試圖解決我的問題的人,我非常感謝。

0

除非我誤解了你,否則你試圖在實例化硬幣的同一位置創建一個敵人。這個問題,因爲我可以看到它,是這些行:

Coinnumber = Coinnumber + Random.Range(5,10); 
Coinnumberside = Coinnumberside + Random.Range(0.2,-0.4); 
... 
EnemyPosition.z = 5.69 + Coinnumber; 
EnemyPosition.x = 0.0 + Coinnumberside; 

你重新計算Coinnumber變量正確設置你的敵人的位置之前。唯一不變的值是Y座標,因爲你重新計算了Z和X.所以,例如,當你第一次運行這個腳本時,你的硬幣會產生在(5.69f,0.4f,0f)的位置,然後enemyPosition會得到隨機值加到這裏,所以它可能(例如)是(10.69f,0.4f,0.2f)

請考慮也許嘗試此:

var StagePosition : Vector3; 
StagePosition.z = 5.69 + Coinnumber; 
StagePosition.x = 0.0 + Coinnumberside; 
StagePosition.y = 0.4; 

Instantiate(coinPrefab, StagePosition, Quaternion.identity); 
Instantiate(enemyPrefab, StagePosition, Quaternion.identity); 

Coinnumber = Coinnumber + Random.Range(5,10); 
Coinnumberside = Coinnumberside + Random.Range(0.2,-0.4); 

這將在相同的位置,你的硬幣產卵敵人,然後計算之後你的下一個硬幣&敵人一個新的位置。但如果這不是你想要做的,讓我知道,我會改變我的答案。

+0

嘿史蒂芬。是的,這是我想要做的。我也嘗試過把Coinnumber和Coinnumberside放到最後,但是,敵人的預製似乎只是沿着Y軸向上和向下實例化,就像以前一樣。 – Edvin

+0

你試過了嗎:'實例化(enemyPrefab,StagePosition,Quaternion.identity);'? –

+0

我很抱歉遲到的回覆,但當時我沒有嘗試這個,但是當我這樣做,它仍然沒有工作,然後我嘗試使用兩個不同的IF語句爲兩個實例,它立即工作。 再次,對於遲到的回覆感到抱歉。 – Edvin