简体   繁体   English

触发器没有响应我在Unity 3D中的需求

[英]Trigger's not responding how I want in unity 3d

I need help with a game I am creating. 我需要有关正在创建的游戏的帮助。 I am building the game with the Unity engine and am pretty new to C# coding. 我正在使用Unity引擎构建游戏,并且对C#编码来说还很陌生。 I am creating a 2d game where the player controls the color of a square and must change it to the correct color as when it passes a certain colored object. 我正在创建一个2D游戏,其中玩家控制正方形的颜色,并且必须像通过某个有色物体时一样将其更改为正确的颜色。 The player changes colors great and the objects are triggers. 播放器会改变颜色,并且对象是触发器。 When a player passes the trigger if its not the right color the player dies. 当玩家通过触发器(如果触发器的颜色不正确)时,该玩家死亡。 Well that works perfect but only for the first object the next one no matter the color of the player the object dies. 很好,这很好,但是无论对象死去的玩家的颜色如何,它仅对于第一个对象是下一个对象。 I have tried if's in if statements and else if's I can't seem to figure it out. 我已经尝试过if语句在if语句中,否则尝试过if语句,我似乎无法弄清楚。 Your help would be greatly appreciated! 您的帮助将不胜感激!

Here is the code for the player 这是播放器的代码

void OnTriggerEnter (Collider other)
{
    if (other.transform.tag == "Blue") {
        blue = true;
    }
    else {
        blue=false;
    }

    if (other.transform.tag == "Red") {
        red = true;
        } 
    else {
        red =false;
    }

    if (other.transform.tag == "Blue" && GameObject.Find ("Blue").GetComponent<Blue> ().mb == false) {
                    yield return 0;
                    Die ();
    } else if (other.transform.tag == "Red" && GameObject.Find ("Red").GetComponent<Red> ().mr == false) {
                    Die ();     
    }

}

Here is the code for each different colored object. 这是每个不同颜色对象的代码。 This one happens to be blue. 这个碰巧是蓝色的。

void Update () {
    if (GameObject.Find ("Player").GetComponent<Movement> ().blue == true && GameObject.Find ("Player").GetComponent<Movement> ().playerBlue == true) {
                    mb = true;  
            } else {
        mb = false;         
    }
    if (!GameObject.Find("Player").GetComponent<Movement>().blue) {
        mb = false; 
    } 
}

Your execution order is a little messy, that's probably why it's not working. 您的执行顺序有点混乱,这可能就是为什么它不起作用的原因。 Try to make all your logic follow one path, not two. 尝试使所有逻辑遵循一条路径,而不是两条路径。 Right now, you are: 现在,您是:

  • (1)Colliding, (2) setting some boolean variables, (3)checking some variables, (4)optionally dying. (1)碰撞,(2)设置一些布尔变量,(3)检查一些变量,(4)可选地死亡。
  • At a separate time in a separate class, set the boolean that was set in step 1 and setting the variable that's checked in step 3 在单独的类中的单独时间,设置在步骤1中设置的布尔值,并设置在步骤3中检查的变量

This is what we call spaghetti code. 这就是我们所说的意大利面条代码。 It's all twisted together. 都扭曲了。 Can you think of a way to untangle it, so the two bits of logic don't mutually depend on each other? 您能想到一种解开逻辑的方法,以使逻辑的这两个位不相互依赖吗?

For example, I see in the latter block of code, you're checking whether the player is blue. 例如,在后面的代码块中,您正在检查播放器是否为蓝色。 Could you instead run that code when the player sets blue (or sets blue=false )? 当播放器设置为blue (或设置blue=false )时,您可以运行该代码吗?

Edit: I know this isn't strictly answering your question, but this untangling is the first step of the problem solving process. 编辑:我知道这并不是严格地回答您的问题,但是,理清一切是解决问题过程的第一步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM