2013-12-07 37 views
0

在我的Unity3D遊戲中,我有不明飛行物的敵人飛來飛去,產生了一些小爪牙等等。我想讓他們比小奴才更難殺人,所以我們在他們的主要劇本中加入了一些保健功能。我這樣做是因爲當玩家在不明飛行物足夠的時間射擊,UFO將被毀滅。 (而不是拍攝一次,然後噗!它不見了。) 這是第一人稱射擊遊戲,由於某種原因它不起作用。更糟的是,如果持續時間足夠長,遊戲就會崩潰並進入灰色屏幕。我已經看過一些腳本論壇,但還沒有找到答案。我可能在某處(使用C#而不是javascript)濫用變量,因爲我不太確定它爲什麼不起作用。敵方健康減少腳本

var UFOspeed : float = 0.2f; //the speed of it's flight 

    var UFOmovement = true; 
    var UFOmovement2 = false; 
    var UFOhealth = 10; //this is the amount of health I added. I'm unsure if in JS you have to put .0f at the end of numbers unless if it's a float. 


    function Start() 
    { 
    UFOmove(); 
    } 

    function Update() //this update just changes the direction of the movement of the UFO 
    { 
     if(UFOmovement == true && UFOmovement2 == false) 
     { 
      this.transform.position.x += UFOspeed; 
     } 

     if(UFOmovement == true && UFOmovement2 == true) 
     { 
      this.transform.position.z += UFOspeed; 
     } 

     if(UFOmovement == false && UFOmovement2 == true) 
     { 
      this.transform.position.x -= UFOspeed; 
     } 

     if(UFOmovement == false && UFOmovement2 == false) 
     { 
      this.transform.position.z -= UFOspeed; 
     } 
    } 



    function UFOmove() //UFO movement 
    { 
     for(i=1;i>0;i++) 
     { 

    yield WaitForSeconds(1); 

    UFOmovement2 = true; 

    yield WaitForSeconds(1); 

    UFOmovement = false; 

    yield WaitForSeconds(1); 

    UFOmovement2 = false; 

    yield WaitForSeconds(1); 

    UFOmovement = true; 

    } 
    } 

     //This is where I have the bullet collision 

     function OnCollisionEnter(collision : Collision) { 
     if (collision.gameObject.tag == "Bullet") //if the tagged object is Bullet 
     { 
      UFOhealth = UFOhealth - 1; //takes away from the health I put above 
     } 

     if (UFOhealth <=0) 
     { 
      Destroy(collision.gameObject, 3.0); 
     } 
    } 

感謝您的幫助!

回答

1

我從來沒有使用unity3d,但看着你的代碼,我可以看到幾個主要問題(在我看來)。

在您的OnCollisionEnter函數中,您檢查碰撞是否與子彈一致。如果是這樣,那麼你從UFO健康中扣除1。到現在爲止還挺好。

然而,你正在檢查健康是否降到0 - 如果是的話,你正在摧毀子彈。相反,我建議不管UFO的健康狀況如何(即在每次擊中子彈後)都要銷燬子彈。此外,如果UFO健康已降到0,則需要銷燬實際的UFO對象。所以,你的代碼會變得這樣的事情:

function OnCollisionEnter(collision : Collision) { 
    if (collision.gameObject.tag == "Bullet") //if the tagged object is Bullet 
    { 
     UFOhealth = UFOhealth - 1; //takes away from the health I put above 
     Destroy(collision.gameObject); 
    } 

    if (UFOhealth <=0) 
    { 
     Destroy(UFOobject, 3.0); 
    } 
} 

請注意,這不是複製粘貼,但只給你的,我認爲你的錯誤是一個想法。

+0

你說得對!我錯過了。在JS中,我會寫Destroy(this.gameObject,3.0);摧毀不明飛行物。 不幸的是,UFO在1次擊中後仍然會自行刪除。至少遊戲不會崩潰(這可能是一個語法錯誤的地方),但希望我能弄明白自己。建議摧毀子彈是一個非常好的主意,謝謝! – user3037531