【问题标题】:How to fix the health bar如何修复健康栏
【发布时间】:2019-04-29 05:10:15
【问题描述】:

我正在创建一个破坏方块的 2D 游戏,并且我正在尝试制作一个健康栏(3 颗星)。每当球击中对撞机时,用户的生命应该会减少,但是,当球击中不可见的对撞机时,它会自动进入“Lose”场景。我试图从另一个脚本中调用变量“健康”,但它不起作用。我知道我做错了什么,但我似乎无法找出它是什么。有什么建议吗?

下图是 LivesStars1、LivesStars2、LivesStars3 的检查器

健康脚本:

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


public class Health : MonoBehaviour {


public GameObject LivesStars1, LivesStars2, LivesStars3;
public static int health;

void Start()
{
    health = 3;
    LivesStars1.gameObject.SetActive(true); //The object LiveStars1 is being enabled
    LivesStars2.gameObject.SetActive(true); //The object LiveStars2 is being enabled
    LivesStars3.gameObject.SetActive(true); //The object LiveStars3 is being enabled

}

void Update()
{
    int health = health;

    switch (health)
    {
        case 3: //If the user doesn't lose a life all 3 lives remain
            LivesStars1.gameObject.SetActive(true); //The object LiveStars1 is being enabled
            LivesStars2.gameObject.SetActive(true); //The object LiveStars2 is being enabled
            LivesStars3.gameObject.SetActive(true); //The object LiveStars3 is being enabled
            break;
        case 2: //If the user loses one life only LivesStars3 is disabled
            LivesStars1.gameObject.SetActive(true);
            LivesStars2.gameObject.SetActive(true);
            LivesStars3.gameObject.SetActive(false);
            break;
        case 1: //If the user loses two lives then LivesStars2 will also be disabled
            LivesStars1.gameObject.SetActive(true);
            LivesStars2.gameObject.SetActive(false);
            LivesStars3.gameObject.SetActive(false);
            break;
        case 0: //If the uses loses all his lives then the Lose scene is enabled
            LivesStars1.gameObject.SetActive(false);
            LivesStars2.gameObject.SetActive(false);
            LivesStars3.gameObject.SetActive(false);
            break;

    }
  }
}

加载场景脚本:

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;

    if(Health.health == 0)
    {
       lvlManager.LoadLevel("Lose");
    }
}



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

}

【问题讨论】:

  • lvlManager.LoadLevel("Lose"); 这正是你所做的。
  • 什么意思?

标签: c# unity3d


【解决方案1】:

问题似乎出在“LoadScenes”脚本中 当某些东西进入触发器时,就会调用 OnTriggerEnter2D。 在检查您的健康是否为 0 之前,您已经切换到“失败”级别。

这是您的原始代码(我在导致此行为的行中添加了注释)

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"); // <-- This here is your problem
    Bricks.brickCount = 0;

    if(Health.health == 0)
    {
       lvlManager.LoadLevel("Lose");
    }
}



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

}

这是应该的:

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");

    Bricks.brickCount = 0;

    if(Health.health == 0)
    {
       lvlManager.LoadLevel("Lose");
    }
}



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

}

TLDR: 在检查玩家健康状况之前,您有一个多余且不必要的lvlManager.LoadLevel("Lose");。

【讨论】:

    猜你喜欢
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-12
    相关资源
    最近更新 更多