我正在制作一个简单的僵尸生存游戏。我对这段代码有一个问题,它只检测一个触发器,但不检测另一个触发器。
#pragma strict
var health = 100;
var attack = 10;
var delay = 5;
var scream : AudioClip;
var player : Collider;
function OnTriggerEnter () {
if (player.gameObject.tag == "ZombieFlame") {
gameObject.Find("Flame").SendMessage("OnTriggerEnter");
}
if (player.gameObject.tag == "Zombie") {
Attack ();
}
if (health == 0) {
Debug.Log("Die!");
Lose ();
}
}
function Attack () {
health -= attack;
Debug.Log("Under attack!");
audio.PlayOneShot(scream);
yield WaitForSeconds(delay);
Loop ();
}
function Loop () {
OnTriggerEnter ();
}
function Lose () {
this.active = false;
}
我的脚本检测到“ZombieFlame”但不是“Zombie”。 gameObjects已经有了标签,所以我不知道发生了什么。它也像Trigger一样被检查。
答案 0 :(得分:1)
您没有在函数函数OnTriggerEnter()中传递任何参数。它应该有一个对撞机参数。像这样使用 -
function OnTriggerEnter (other : Collider) {
if (other.gameObject.tag == "ZombieFlame") {
gameObject.Find("Flame").SendMessage("OnTriggerEnter");
//It may give error for the Flame's OnTriggerEnter() function without
//parameter. I don't really get why are you sending message to Flame.
//You can remove this if the Flame has script attached containing
//OnTriggerEnter(other : Collider). And check if 'other' is this player or
//other gameobject collider as this function. It will give you more control.
}
if (other.gameObject.tag == "Zombie") {
Attack ();
}
if (health == 0)
Debug.Log("Die!");
Lose ();
}
}