使玩家使用固定数量的生命值?

时间:2018-11-15 11:50:28

标签: c# unity3d 2d

嘿,当我的玩家受到伤害并失去健康时,我想使他不犯罪。自动取款机我不能使我的玩家不被打败,而且如果他与几个伤害结构相撞,他所遭受的伤害会超出我的预期,我希望他失去20%的生命值,然后在2-3秒内立于不败之地。

这是附加到我的伤害结构上的

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

    public class LoseHealth : MonoBehaviour {



       private void OnTriggerEnter2D(Collider2D collision)
        {
            if (collision.CompareTag("Player"))
            {
                HealthBarScript.health -= 20f;
            }

        }
    }

这是给我小时候依附在我“空虚的心”上的画布上的FullHearts

  using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class HealthBarScript : MonoBehaviour {


    Image FullHearts;
    float maxHealth = 100f;
    public static float health;
    // Use this for initialization
    void Start () {
        FullHearts = GetComponent<Image>();
        health = maxHealth;
    }

    // Update is called once per frame
    void Update () {
        FullHearts.fillAmount = health / maxHealth;
        if (health == 0)
        {
            Application.LoadLevel(Application.loadedLevel);
        }
    }
}

ATM我在我的播放器脚本上有此提示,该脚本无法正常工作,所有其他脚本都在工作,但无论播放器碰到多种结构,然后无暇恢复并没有击中提示框,都只需要1心就可以输结构:

  if (!invincible)
        {
            if (col.gameObject.tag == "enemy")
            {
               // HealthBarScript.health -= 20f;
                // health -= 20; // subtract 1 form your total health
                invincible = true; // makes this whole function unusable since invincible is no longer false
                new WaitForSeconds(3);
                invincible = false; // makes this whole function reusable since invincible is false again
            }


        }

1 个答案:

答案 0 :(得分:1)

因此,我使用您提供的方法进行了简单的碰撞“游戏”,一切正常。但是现在,当我进一步看时,我发现您提供的内容有些冗余,您的“答案”有点令人困惑。

但是您的第一个脚本显示,伤害发动者每次与“玩家”碰撞时,都会造成伤害。

private void OnTriggerEnter2D(Collider2D collision)
        {
            if (collision.CompareTag("Player"))
            {
                HealthBarScript.health -= 20f;
            }

        }

并且每次“玩家”与“敌人”发生冲突时,它都会进行一次无敌的检查,该检查应该起作用。 (假设注释已删除)。但是从评论的角度来看,“玩家”击中了伤害结构并不会造成任何伤害,但是从结构到玩家总是(无论无敌是什么)伤害。

if (!invincible)
    {
        if (col.gameObject.tag == "enemy")
        {
            // HealthBarScript.health -= 20f;
            // health -= 20; // subtract 1 form your total health
            invincible = true; // makes this whole function unusable since invincible is no longer false
            StartCoroutine(Invincible());                   // makes this whole function reusable since invincible is false again
        }


    }
}


IEnumerator Invincible()
{
    if (invincible == true)


    {
        yield return new WaitForSeconds(2);
        {


            invincible = false;
        }
    }

但是当您在无敌的支票到期之前再次击中敌人的剧本时,它仍然在造成伤害。

您有两个脚本会造成损坏,只有一个脚本会进行无敌检查。

摆脱结构上的破坏性脚本,正确标记所有结构,在播放器上对脚本进行注释,您将很轻松。或者,您可以将脚本保留在损坏结构上,并通过使该布尔值保持静态来让每个玩家检查玩家是否立于不败之地。

使用Debug.Log(“到底发生了什么”)的标准用法,您也可以轻松回答自己的问题。 Debug.Log应该是您最好的朋友。

相关问题