我正在为一个块破坏者游戏做一个带有精灵图像的健康计数器。问题是,当球击中对撞机时,他会自动进入失落场景而不会失去任何生命。每当球接触对撞机时,用户应该失去生命,当他用完生命时,用户会进入失去的场景,但这些都不起作用。
我不知道我是想改变一些东西还是在我的一个脚本中添加一些内容。有谁可以帮助我吗?谢谢。
生活脚本:
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);
}
}
据我在LoadScene脚本中注意到:
void OnTriggerEnter2D()
{
print("The wall is triggered by the ball");
lvlManager.LoadLevel("Lose");
Bricks.brickCount = 0;
}
你基本上告诉它立即触发Lose游戏场景,而你应该调用一个函数来减少一个心脏,然后更新它将检查更新的心脏数量的健康状况,然后让它决定游戏是否丢失