Forum begins after the advertisement:


[General] About time freeze

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [General] About time freeze

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #15613
    MI NI
    Level 14
    Bronze Supporter (Patron)
    Helpful?
    Up
    0
    ::

    Last time I asked you about how to freeze time when opening inventory and maps, I had some questions. My map would remain there after it was opened and could not be closed. Can you give me some advice? The article I was recommended to refer to last time

    Here are my steps

    1. Add in GameManager.cs
    <code>    [SerializeField] private FadeUI mapPause;
    
    if(Input.GetButtonDown("Map") && PlayerController.Instance.openMap)
            {
                mapPause.FadeUIIn(fadeTime);
                Time.timeScale = 0;
                GameManager.Instance.gameIsPaused = true;
            }
            else if (Input.GetButtonDown("Map") && !PlayerController.Instance.openMap)
            {
                mapPause.FadeUIOut(fadeTime);
                UnpauseGame();
            }</code>
    1. Add FadeUI.cs & Canvas Group to Unity MapHandler

    2. Drag MapHandler into GameManager’s mapPause in Unity

    #15617
    Joseph Tang
    Level 13
    Moderator
    Helpful?
    Up
    0
    ::

    Let’s try this addition of code:

            if(Input.GetButtonDown("Map") && PlayerController.Instance.openMap && !GameManager.Instance.gameIsPaused)
            {
                mapPause.FadeUIIn(fadeTime);
                Time.timeScale = 0;
                GameManager.Instance.gameIsPaused = true;
            }
            else if (Input.GetButtonDown("Map") && !PlayerController.Instance.openMap && GameManager.Instance.gameIsPaused)
            {
                mapPause.FadeUIOut(fadeTime);
                UnpauseGame();
            }
    #15625
    MI NI
    Level 14
    Bronze Supporter (Patron)
    Helpful?
    Up
    0
    ::

    it doesn’t work, I made the above modifications, but it will freeze completely and become inoperable.

    #15626
    Joseph Tang
    Level 13
    Moderator
    Helpful?
    Up
    0
    ::

    Okay remove that code, I want to check something, do you still have your input for the Map button in the PlayerController.cs?

    Furthermore, can you show how your map looks like in the Scene’s Hierarchy?

    Considering what it is, i believe the map is on 24/7 and PlayerController.Instance.openMap may be set to true without being set false one way or the other.

    #15631
    MI NI
    Level 14
    Bronze Supporter (Patron)
    Helpful?
    Up
    0
    ::

    Okay, my PlayerController has input for the map button, and this is my video. In the video, I keep clicking the map button. I guess it tries to close?

    #15633
    Joseph Tang
    Level 13
    Moderator
    Helpful?
    Up
    0
    ::
            if(Input.GetButtonDown("Map") && !PlayerController.Instance.openMap)
            {
                mapPause.FadeUIIn(fadeTime);
                Time.timeScale = 0;
                GameManager.Instance.gameIsPaused = true;
            }
            else if (Input.GetButtonDown("Map") && !PlayerController.Instance.openMap)
            {
                mapPause.FadeUIOut(fadeTime);
                UnpauseGame();
            }

    Can you try this?

    #15637
    MI NI
    Level 14
    Bronze Supporter (Patron)
    Helpful?
    Up
    0
    ::

    I made the above modifications. After I press the map button for the first time, it will disappear by itself. After pressing the button again, an error message will be generated.

    #15638
    Joseph Tang
    Level 13
    Moderator
    Helpful?
    Up
    0
    ::

    Mhm, I would likely believe that the issue behind this is caused by there being two codes searching for the map input, where one of them is dependent on the openMap bool that’s activated by the other.

    In other words, I believe it’ll be best if you compile all the related code for turning the map on and off into one script. Which would probably be better in the GameManager.cs.

    Can you try moving the code for activating openMap in PlayerController.cs into the code you use in GameManager.cs?

    #15639
    MI NI
    Level 14
    Bronze Supporter (Patron)
    Helpful?
    Up
    0
    ::

    I think I have a big problem, I can’t pause the game while I’m porting the switch map to GameManager, but I decided to finish the switch map first, and then make the time freeze, but after I port the code to GameManager my map Something went wrong, it was completely invisible, I couldn’t even see the map from the Scene screen, and the SceneTransition suddenly had problems. I restored the map part to PlayerController to no avail, it is still invisible.

    Update, I have no problem with SceneTransition. I was too nervous about the Map problem and accidentally turned off SceneFader without enabling it.

    update again, my map disappears as if I accidentally adjusted it to Alpha value. So the above problem is solved,

    <code>using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    
    public class GameManager : MonoBehaviour
    {
        public string transitionedFromScene;  //儲存前一個場景
    
        public Vector2 platformingRespawnPoint; //重生點
        public Vector2 respawnPoint;
        [SerializeField] SavePoint savePoint;
    
        [SerializeField] private FadeUI pauseMenu;
        [SerializeField] private FadeUI mapPause;
        [SerializeField] private float fadeTime;
        public bool gameIsPaused;
        //public bool Resumed = false;
    
        bool openMap = false;
    
        public static GameManager Instance { get; private set; }
        private void Awake()
        {
            SaveData.Instance.Initialize();
    
            if(Instance != null && Instance != this)
            {
                Destroy(gameObject);
            }
            else
            {
                Instance = this;
            }
    
            SaveScene();
    
            DontDestroyOnLoad(gameObject);
    
            SaveData.Instance.LoadSceneData();
    
            SaveData.Instance.LoadMapData();
    
            savePoint = FindObjectOfType<SavePoint>();
    
        }
    
        private void Update()
        {
    
            if (Input.GetKeyDown(KeyCode.Escape) && !gameIsPaused)
            {
                pauseMenu.FadeUIIn(fadeTime);
                Time.timeScale = 0;
                GameManager.Instance.gameIsPaused = true;
            }
    
            if (Input.GetButtonDown("Map") && !openMap)
            {
                openMap = true;
            }
            else if (Input.GetButtonDown("Map") && openMap)
            {
                openMap = false;
            }
        }
    
        public void ToggleMap()
        {
            if (openMap)
            {
                //mapPause.FadeUIIn(fadeTime);
                //Time.timeScale = 0;
                //GameManager.Instance.gameIsPaused = true;
                UIManager.Instance.mapHandler.SetActive(true);
            }
            else
            {
                //mapPause.FadeUIOut(fadeTime);
                //UnpauseGame();
                UIManager.Instance.mapHandler.SetActive(false);
            }
        }
    
        public void SaveGame()
        {
            SaveData.Instance.SavePlayerData();
        }
    
        public void UnpauseGame()
        {
            //Resumed = true;
            Time.timeScale = 1;
            GameManager.Instance.gameIsPaused = false; //沒有正確銷毀gamemanager 導致需要加GameManager.Instance
            Debug.Log(gameIsPaused);
        }
    
        public void SaveScene()
        {
            //獲取當前場景名稱
            string currentSceneName = SceneManager.GetActiveScene().name;
            SaveData.Instance.sceneName.Add(currentSceneName);
            SaveData.Instance.SaveSceneData();
    
            //Debug.Log("saved" + currentSceneName);
        }
    
        public void RespawnPlayer()
        {
            SaveData.Instance.LoadSavePoint();
    
            if(SaveData.Instance.savePointSceneName != null) //載入重生點的場景
            {
                print(SaveData.Instance.savePointSceneName + "by GameManager");
                SceneManager.LoadScene(SaveData.Instance.savePointSceneName);
            }
    
            if(SaveData.Instance.savePointPos != null) //設定重生位置為savePoint
            {
                respawnPoint = SaveData.Instance.savePointPos;
            }
            else
            {
                respawnPoint = platformingRespawnPoint;
            }
    
            PlayerController.Instance.transform.position = respawnPoint;
    
            StartCoroutine(UIManager.Instance.DeactiveateDeathScreen()); //重生點淡入淡出
            PlayerController.Instance.Respawned(); //使角色重生
        }
    }
    </code>

    PlayController.cs Add GameManager.Instance.ToggleMap(); to PlayerController

    <code>void Update()
        {
            if (GameManager.Instance.gameIsPaused) return;
    
            RestoreTimeScale();
    
            if (pState.cutscene) return;
            if (pState.alive)
            {
                GetInputs();
                //ToggleMap();
                GameManager.Instance.ToggleMap();
                ToggleInventory();
            }
            UpdateJumpVariables();
            RestoreTimeScale();
            UpdateCameraYDampForPlayerFall();
    
            if (pState.alive)
            {
                Heal();
            }
    
            if (pState.dashing || pState.healing) return;
    
            if (pState.alive)
            {
                if (!isWallJumping)
                {
                    Flip();
                    Move();
                    Jump();
                }
                if (unlockWallJump)
                {
                    WallSlide();
                    WallJump();
                }
                if (unlockDash)
                {
                    StartDash();
                }
    
                Attack();
                CastSpell();
            }
            FlashWhileInvincible();
        }</code>
    #15640
    MI NI
    Level 14
    Bronze Supporter (Patron)
    Helpful?
    Up
    0
    ::

    I modified the GameManager to look like this, but it gave me the error message

    <code>Coroutine couldn't be started because the game object 'MapHandler' is inactive!
    UnityEngine.MonoBehaviour:StartCoroutine (System.Collections.IEnumerator)</code>

    can you give me some advice Additionally I call ToggleMap() in PlayerController

    <code>using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    
    public class GameManager : MonoBehaviour
    {
        public string transitionedFromScene;  //儲存前一個場景
    
        public Vector2 platformingRespawnPoint; //重生點
        public Vector2 respawnPoint;
        [SerializeField] SavePoint savePoint;
    
        [SerializeField] private FadeUI pauseMenu;
        [SerializeField] private FadeUI mapPause;
        [SerializeField] private float fadeTime;
        public bool gameIsPaused;
        //public bool Resumed = false;
    
        bool openMap = false;
    
        public static GameManager Instance { get; private set; }
        private void Awake()
        {
            SaveData.Instance.Initialize();
    
            if(Instance != null && Instance != this)
            {
                Destroy(gameObject);
            }
            else
            {
                Instance = this;
            }
    
            SaveScene();
    
            DontDestroyOnLoad(gameObject);
    
            SaveData.Instance.LoadSceneData();
    
            SaveData.Instance.LoadMapData();
    
            savePoint = FindObjectOfType<SavePoint>();
    
        }
    
        private void Update()
        {
    
            if (Input.GetKeyDown(KeyCode.Escape) && !gameIsPaused)
            {
                pauseMenu.FadeUIIn(fadeTime);
                Time.timeScale = 0;
                GameManager.Instance.gameIsPaused = true;
            }
    
            if (Input.GetButtonDown("Map") && !openMap)
            {
                openMap = true;
                mapPause.FadeUIIn(fadeTime);
                Time.timeScale = 0;
                GameManager.Instance.gameIsPaused = true;
            }
            else if (Input.GetButtonDown("Map") && openMap)
            {
                openMap = false;
                mapPause.FadeUIOut(fadeTime);
                UnpauseGame();
            }
        }
    
        public void ToggleMap()
        {
            if (openMap)
            {
                UIManager.Instance.mapHandler.SetActive(true);
            }
            else
            {
                UIManager.Instance.mapHandler.SetActive(false);
            }
        }
    
        public void SaveGame()
        {
            SaveData.Instance.SavePlayerData();
        }
    
        public void UnpauseGame()
        {
            //Resumed = true;
            Time.timeScale = 1;
            GameManager.Instance.gameIsPaused = false; //沒有正確銷毀gamemanager 導致需要加GameManager.Instance
            Debug.Log(gameIsPaused);
        }
    
        public void SaveScene()
        {
            //獲取當前場景名稱
            string currentSceneName = SceneManager.GetActiveScene().name;
            SaveData.Instance.sceneName.Add(currentSceneName);
            SaveData.Instance.SaveSceneData();
    
            //Debug.Log("saved" + currentSceneName);
        }
    
        public void RespawnPlayer()
        {
            SaveData.Instance.LoadSavePoint();
    
            if(SaveData.Instance.savePointSceneName != null) //載入重生點的場景
            {
                print(SaveData.Instance.savePointSceneName + "by GameManager");
                SceneManager.LoadScene(SaveData.Instance.savePointSceneName);
            }
    
            if(SaveData.Instance.savePointPos != null) //設定重生位置為savePoint
            {
                respawnPoint = SaveData.Instance.savePointPos;
            }
            else
            {
                respawnPoint = platformingRespawnPoint;
            }
    
            PlayerController.Instance.transform.position = respawnPoint;
    
            StartCoroutine(UIManager.Instance.DeactiveateDeathScreen()); //重生點淡入淡出
            PlayerController.Instance.Respawned(); //使角色重生
        }
    }
    </code>
    #15641
    MI NI
    Level 14
    Bronze Supporter (Patron)
    Helpful?
    Up
    0
    ::

    After I thought about it carefully, I planned to delete ToggleMap() and integrate it into the same place for judgment. In addition, I found that FadeUI.cs may not be needed here. I will share with you the code that I successfully tested. I also added !gameIsPause has been added to the map button judgment to avoid overlapping with other pause screens and causing multiple screens with different functions to appear at the same time. I don’t know if this is correct. There may be problems that I didn’t expect.

    <code>//地圖Map
            if (Input.GetButtonDown("Map") && !openMap && !gameIsPaused)
            {
                openMap = true;
    
                if (openMap)
                {
                    Time.timeScale = 0;
                    GameManager.Instance.gameIsPaused = true;
                }
                UIManager.Instance.mapHandler.SetActive(true);
            }
            else if (Input.GetButtonDown("Map") && openMap)
            {
                openMap = false;
    
                if (!openMap)
                {
                    UnpauseGame();
                }
                UIManager.Instance.mapHandler.SetActive(false);
            }
    
            //背包Inventory
            if (Input.GetButtonDown("Inventory") && !openInventory && !gameIsPaused)
            {
                openInventory = true;
    
                if(openInventory)
                {
                    Time.timeScale = 0;
                    GameManager.Instance.gameIsPaused = true;
                }
                UIManager.Instance.inventory.SetActive(true);
            }
            else if (Input.GetButtonDown("Inventory") && openInventory)
            {
                openInventory = false;
    
                if(!openInventory)
                {
                    UnpauseGame();
                }
                UIManager.Instance.inventory.SetActive(false) ;
            }</code>
Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: