【问题标题】:OnTriggerEnter can't detect my Transform tagOnTriggerEnter 无法检测到我的 Transform 标签
【发布时间】:2015-11-06 04:09:42
【问题描述】:

我正在制作一个简单的僵尸生存游戏。这段代码有问题,它只检测到一个触发器,而不是另一个。

#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 一样被检查。

【问题讨论】:

  • “那个Transform已经有了标签”——我不明白这是什么意思。
  • 您使用的所有游戏对象的具体配置是什么?
  • 我的英语不好...我想说“转换已经有标签”

标签: unity3d unityscript


【解决方案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 ();
    }
}

【讨论】:

  • 我已经尝试过了...我收到一个错误:Health.OnTriggerEnter(UnityEngine.Collider) 方法的最佳重载与参数列表“()”不兼容。
  • Health 类是什么样的?你在哪一行得到错误?我认为你必须在这里和那里改变很多东西才能继续工作。
  • 你说得对......问题是函数循环。我要创建一个布尔状态“isAttacked”......我对此了解不多,但感谢您的帮助。问候。
猜你喜欢
  • 2015-03-07
  • 1970-01-01
  • 2022-08-24
  • 2021-03-02
  • 1970-01-01
  • 1970-01-01
  • 2012-03-18
  • 1970-01-01
  • 2020-02-06
相关资源
最近更新 更多