C# 统一:玩家对象被摧毁后暂停游戏

C# 统一:玩家对象被摧毁后暂停游戏,c#,unity3d,unity5,C#,Unity3d,Unity5,正如标题所说,我想这样做,当玩家游戏对象被破坏时,游戏将暂停并出现一个屏幕(我已经制作了一个名为GameOverScreen的屏幕),然而,我似乎无法集中注意力。屏幕显示,但游戏不会暂停。你知道这是什么原因吗?这是我的密码: using UnityEngine; using UnityEngine.UI; public class PlayerHealth : MonoBehaviour { public float m_StartingHealth = 100f;

正如标题所说,我想这样做,当玩家游戏对象被破坏时,游戏将暂停并出现一个屏幕(我已经制作了一个名为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);
    }
}
使用UnityEngine;
使用UnityEngine.UI;
公共级玩家健康:单一行为
{
公共浮子m_StartingHealth=100f;//每个油箱的起始健康量。
公共滑块m_Slider;//表示坦克当前有多少生命值的滑块。
public Image m_FillImage;//滑块的图像组件。
public Color m_FullHealthColor=Color.green;//处于完全健康状态时健康栏的颜色。
公共颜色m_ZeroHealthColor=Color.red;//启用“无运行状况”时运行状况栏的颜色。
public GameObject m_explosionprefable;//一个将在唤醒中实例化的预置,然后在坦克死亡时使用。
公共摄像机;
公共摄像机游戏机;
公共游戏对象GameOverMenu;
private bool gameover=假;
私有音频源m_ExplosionAudio;//坦克爆炸时要播放的音频源。
私有粒子系统m_ExplosionParticles;//当坦克被摧毁时,粒子系统将播放。
private float m_CurrentHealth;//坦克当前的生命值。
二等兵布尔穆死了;//坦克的生命值已经降到零以上了吗?
void Start()
{
GameOverMenu.SetActive(假);
}
私人空间
{
//实例化爆炸预设并获取对其上粒子系统的引用。
m_ExplosionParticles=实例化(m_ExplosionPrefab).GetComponent();
//获取对实例化预置上音频源的引用。
m_ExplosionAudio=m_ExplosionParticles.GetComponent();
//禁用预置,以便在需要时激活它。
m_ExplosionParticles.gameObject.SetActive(false);
}
私有void OnEnable()
{
//启用油箱后,重置油箱的运行状况以及是否已死亡。
m_CurrentHealth=m_StartingHealth;
死亡=错误;
//更新健康滑块的值和颜色。
SetHealthUI();
}
公共损失(浮动金额)
{
//减少当前生命值所造成的伤害。
m_CurrentHealth-=金额;
//适当地更改UI元素。
SetHealthUI();
//如果当前运行状况等于或低于零且尚未注册,请调用OnPath。

if(m_CurrentHealth首先,您不需要两个if,只需执行以下操作:

if (gameover) {
   Time.timeScale = 0;
}else{
   Time.timeScale = 1;
}
接下来,添加以下内容:

void OnPauseGame ()
{
   GameOverMenu.SetActive(true);
}
如果仍然不起作用,则添加:

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

首先,你不需要两个if,只要做:

if (gameover) {
   Time.timeScale = 0;
}else{
   Time.timeScale = 1;
}
接下来,添加以下内容:

void OnPauseGame ()
{
   GameOverMenu.SetActive(true);
}
如果仍然不起作用,则添加:

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

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

Time.timeScale = 0.0f;

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

Time.timeScale = 0.0f;

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

Debug.LogError("Game Paused");

祝你好运

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

Debug.LogError("Game Paused");

祝您好运

“GameOverMenu”的第一个字母必须小写,
GameOverMenu
只需在新场景中加载gameover屏幕即可。无需代码要求“GameOverMenu”的第一个字母必须小写,
GameOverMenu
只需在新场景中加载gameover屏幕即可。无需代码要求