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