【问题标题】:Unity: Pausing the game after player object gets destroyedUnity:玩家对象被破坏后暂停游戏
【发布时间】:2016-04-13 15:51:16
【问题描述】:

正如标题所说,我想这样当玩家的游戏对象被破坏时,游戏会暂停并显示一个屏幕(我制作了一个名为 GameOverScreen 的屏幕),但是,我似乎无法得到我的头绕着它。屏幕显示,但游戏不会暂停。知道是什么原因造成的吗?这是我的代码:

using UnityEngine;
using UnityEngine.UI;

public class PlayerHealth : MonoBehaviour
{
    public float m_StartingHealth = 100f;               // The amount of health each tank starts with.

    public Slider m_Slider;                             // The slider to represent how much health the tank currently has.

    public Image m_FillImage;                           // The image component of the slider.

    public Color m_FullHealthColor = Color.green;       // The color the health bar will be when on full health.

    public Color m_ZeroHealthColor = Color.red;         // The color the health bar will be when on no health.

    public GameObject m_ExplosionPrefab;                // A prefab that will be instantiated in Awake, then used whenever the tank dies.

    public Camera mainCamera;

    public Camera gameOverCamera;

    public GameObject GameOverMenu;

    private bool gameover = false;
    private AudioSource m_ExplosionAudio;               // The audio source to play when the tank explodes.
    private ParticleSystem m_ExplosionParticles;        // The particle system the will play when the tank is destroyed.
    private float m_CurrentHealth;                      // How much health the tank currently has.
    private bool m_Dead;                                // Has the tank been reduced beyond zero health yet?

    void Start()
    {
        GameOverMenu.SetActive(false);
    }

    private void Awake()
    {
        // Instantiate the explosion prefab and get a reference to the particle system on it.
        m_ExplosionParticles = Instantiate(m_ExplosionPrefab).GetComponent<ParticleSystem>();

        // Get a reference to the audio source on the instantiated prefab.
        m_ExplosionAudio = m_ExplosionParticles.GetComponent<AudioSource>();

        // Disable the prefab so it can be activated when it's required.
        m_ExplosionParticles.gameObject.SetActive(false);
    }


    private void OnEnable()
    {
        // When the tank is enabled, reset the tank's health and whether or not it's dead.
        m_CurrentHealth = m_StartingHealth;
        m_Dead = false;

        // Update the health slider's value and color.
        SetHealthUI();
    }


    public void TakeDamage(float amount)
    {
        // Reduce current health by the amount of damage done.
        m_CurrentHealth -= amount;

        // Change the UI elements appropriately.
        SetHealthUI();

        // If the current health is at or below zero and it has not yet been registered, call OnDeath.
        if (m_CurrentHealth <= 0f && !m_Dead)
        { 
            {
                gameover = !gameover;
            }


            if (gameover)
            {
                GameOverMenu.SetActive(true);
                Time.timeScale = 0;
            }

            if (!gameover)
            {
                GameOverMenu.SetActive(false);
                Time.timeScale = 1;
            }
            OnDeath();
            mainCamera.transform.parent = null;
            mainCamera.enabled = true;
            gameOverCamera.enabled = false;

        }
    }


    private void SetHealthUI()
    {
        // Set the slider's value appropriately.
        m_Slider.value = m_CurrentHealth;

        // Interpolate the color of the bar between the choosen colours based on the current percentage of the starting health.
        m_FillImage.color = Color.Lerp(m_ZeroHealthColor, m_FullHealthColor, m_CurrentHealth / m_StartingHealth);
    }


    private void OnDeath()
    {
        // Set the flag so that this function is only called once.
        m_Dead = true;

        // Move the instantiated explosion prefab to the tank's position and turn it on.
        m_ExplosionParticles.transform.position = transform.position;
        m_ExplosionParticles.gameObject.SetActive(true);

        // Play the particle system of the tank exploding.
        m_ExplosionParticles.Play();

        // Play the tank explosion sound effect.
        m_ExplosionAudio.Play();

        // Turn the tank off.
        gameObject.SetActive(false);
    }
}

【问题讨论】:

  • "GameOverMenu" 第一个字母必须小写,gameOverMenu
  • 只需在新场景中加载游戏结束画面。无需代码

标签: c# unity3d unity5


【解决方案1】:

像这样设置一个精确的浮点值,

Time.timeScale = 0.0f;

【讨论】:

    【解决方案2】:

    首先,您不需要两个 if,只需这样做:

    if (gameover) {
       Time.timeScale = 0;
    }else{
       Time.timeScale = 1;
    }
    

    接下来,添加这个:

    void OnPauseGame ()
    {
       GameOverMenu.SetActive(true);
    }
    

    如果还是不行,再补充:

    void Update(){
       if(Time.timeScale == 0)return;
    }
    

    【讨论】:

      【解决方案3】:

      我认为在发生事件时暂停游戏的最简单方法是使用控制台上的“错误暂停”按钮。 当然,您将需要生成一个错误,但这很容易。 只需使用

      Debug.LogError("Game Paused");
      

      祝你好运

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-31
        • 2022-01-10
        • 2022-01-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多