使玩家丧命

时间:2019-04-28 13:37:41

标签: c# visual-studio unity3d

我正在用块状破碎器游戏的sprite图像进行运行状况计数器。问题在于,当球击中对撞机时,他会自动进入失败场景而不会丧生。每次球碰到对撞机时,用户都应该丧命,当他用尽生命时,用户会进入丢失现场,但是这些都没有起作用。

我不知道我是要更改内容还是向其中一个脚本添加内容。有谁可以帮助我吗?谢谢。

下面的图像供您查看球检查器 Ball ins[pector

下面的图像供您查看心脏1,2,3和4检查器 enter image description here

生活脚本:

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

public class Lives : MonoBehaviour
{
public int health;
public int NumberOfHearts;
public LoadScenes LoadLevel;
public LevelManager lvlManager;
public Image[] hearts; //Creating an arry.
public Sprite FullHeart;
public Sprite EmptyHeart;


void Update()
{
    if(health > NumberOfHearts) //This is to make sure that the ball doesn't go over 4 lives
    {
        health = NumberOfHearts;
    }

    for (int i = 0; i < hearts.Length; i++) //Variable i is less than the length of hearts 
    {
        if (i < health) //If i is smaller than the health
        {
            hearts[i].sprite = FullHeart; //The sprite of FullHeart is displayed
        }
        else
        {
            hearts[i].sprite = EmptyHeart; //The sprite of EmptyHeart is displayed
        }

        if (i < NumberOfHearts)//For loop to check if i is smaller than the NumberOfHearts
        {
            hearts[i].enabled = true; //If i IS smaller than the NumberOfHearts than hearts of the index i is visiable
        }
        else
        {
            hearts[i].enabled = false; //If it ISN'T smaller than the NumberOfHearts than hearts of the index i is invisiable
        }

        if (NumberOfHearts < 0)
        {
            lvlManager.LoadLevel("Lose");
        }

     }

  }

}

LoadScene脚本:

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

public class LoadScenes : MonoBehaviour {

public LevelManager lvlManager;

//If the ball hits one of the walls the colliders will be triggered
void OnTriggerEnter2D()
{
    print("The wall is triggered by the ball");
    lvlManager.LoadLevel("Lose");
    Bricks.brickCount = 0;


}



  void OnCollisionEnter2D()
   {
      Debug.Log("The ball has collided with the wall");
   }

}

LevelManager脚本:

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

public class LevelManager : MonoBehaviour {
  public void LoadLevel(string name)
  {
    print("Level loading requested for" + name);
    SceneManager.LoadScene(name);
   }
}

1 个答案:

答案 0 :(得分:0)

据我在您的LoadScene脚本中所注意到的:

void OnTriggerEnter2D()
{
    print("The wall is triggered by the ball");
    lvlManager.LoadLevel("Lose");
    Bricks.brickCount = 0;
}

您基本上告诉它立即触发“输掉游戏”场景,而您应该调用一个减少心跳的函数,然后更新运行状况,在此函数中它将检查更新的心跳数,然后让它确定游戏是否输了是否