简体   繁体   English

涂鸦跳跃统一C#

[英]Doodle jump unity c#

So look: I have a monster in the game that when player collides with him a panel appears and says try again. 所以看:我在游戏中有一个怪物,当玩家与他发生碰撞时,会出现一个面板并说再试一次。 There is now a shield object, that will protect the player from death, so when the player collides with it and then if you touch the monster you shouldn't die for one time: it will be like that, you touch the shield and you are protected from one death. 现在有一个盾牌对象,可以保护玩家免受死亡的伤害,因此当玩家与之碰撞时,如果您触摸怪物,则不应一次死亡:就像那样,您可以触摸盾牌并且您被保护免于一死。 Can you help me write this code? 你能帮我写这段代码吗?

public GameObject panel;
public bool hasShield = false; /* no shield in the beginning */    

void OnCollisionEnter(Collision col)
{
    if(col.gameObject.tag == "Shield") 
    {
        hasShield = true; //We are safe now.
        /* TODO: StartCoroutine() or Invoke() to reset the variable and the graphic effect after some amount of time. */
    }
    else if (col.gameObject.tag == "Monster" && !hasShield) 
    { 
        //We hit a monster and had no shield. Display gameover.
        panel.SetActive (true);
    }
    //I assume the panel is inactive by default so no need to call SetActive(false) on it.
}

The inside of this code can never be reached: 此代码的内部永远无法到达:

     if (col.gameObject.tag == "Shield") 
    {
        if(col.gameObject.tag == "Monster")
        {
            panel.SetActive(false);
        }
    }

For the first if check to pass, the tag must be "Shield", however for the second test to pass, it must be "Monster". 对于第一个if检查通过,标签必须是“盾牌”,但第二次测试通过,它必须是“怪物”。 These two conditions cannot be true at the same time, the code is equivalent to 这两个条件不能同时为真,代码等同于

 if(col.gameObject.tag == "Shield" && col.gameObject.tag == "Monster")

However, if you replace && with || 但是,如果将&&替换为|| (logical OR) in the expression above, you will get your desired results. (逻辑或)在上面的表达式中,您将获得所需的结果。 So, the rewritten code would be 因此,重写的代码将是

    if (col.gameObject.tag == "Monster") 
    { 
        panel.SetActive (true);
    }
    if (col.gameObject.tag == "Shield" || col.gameObject.tag == "Monster") 
    {
         panel.SetActive(false);
    }

The inside code will now trigger if the is "Shield" or "Monster". 现在,如果“ Shield” “ Monster”,则内部代码将触发。

EDIT: As we've talked about it in the comments, the logic was flawed overall. 编辑:正如我们在评论中谈到的那样,逻辑总体上存在缺陷。 Now according to your description 现在根据您的描述

The panel needs to be activated after i touch the monster, but if i touch the shield and then the monster then the panel should not appear 我碰到怪物后需要激活面板,但是如果我碰到盾牌然后再碰到怪物,那么面板就不会出现

I advise the following: When a shield is collected, save this to some variable, like 我建议:收集盾牌后,将其保存到一些变量中,例如

bool hasShield;

And set it to true when the shield is collected. 并在收集防护罩时将其设置为true Then, if you hit something, check if it is a monster AND if you don't have a shield, then you see the "Game Over" screen. 然后,如果您击中了东西,请检查它是否是怪物,并且如果您没有护盾,那么您会看到“游戏结束”屏幕。 So alltogether the player should look something like 所以总的来说,玩家应该看起来像

    public GameObject panel;
    public bool hasShield = false; /* no shield in the beginning */    

    void OnCollisionEnter(Collision col)
    {
        if(col.gameObject.tag == "Shield") 
        {
             hasShield = true; //We are safe now.
             /* TODO: StartCoroutine() or Invoke() to reset the variable and the graphic effect after some amount of time. */
        }
        else if (col.gameObject.tag == "Monster") 
        { 
            if(!hasShield)
                panel.SetActive (true);  //We hit a monster and had no shield. Display gameover.

            else
                hasShield = false; //loose the shield
        }
        //I assume the panel is inactive by default so no need to call SetActive(false) on it.
    } 

this is the corect code! 这是corect代码!

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class DestroyPlayerOnTouch : MonoBehaviour {
    public GameObject panel;
       bool hasshield = false;

    void OnCollisionEnter(Collision col)
    {
          if (col.gameObject.tag == "Monster"  && !hasshield) 
        { 
            panel.SetActive (true);
        }

        else if(col.gameObject.tag == "Monster" && hasshield)
        {
            panel.SetActive (false);
        }
    }

    void OnTriggerEnter(Collider other) 
    {
        if (other.gameObject.tag == "Shield") 
        {
            Debug.Log("hasshield!");
            hasshield = true;
        }
    }
}

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

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