Home>

[]
After stopping the entire screen for a certain amount of time in the game over process of the action game,
I want to resume subsequent processing. (Screen transition here)

[I am in trouble]
After stopping at Time.timeScale = 0.0f;as shown in the code below,
After executing the Wait process in the coroutine,
I made the logic to return the screen update with Time.timeScale = 1.0f ;, but
Like the comments below, it does n’t work,
I don't know where to put Time.timeScale = 1.0f;

When Time.timeScale = 1.0f;is added, the screen will not pause.

By the way, as for the way of using coroutine, I checked various information sites and tried it.

Teach someone please. m (_ _) m

If there are any deficiencies listed, if you have any questions, we will provide a supplementary explanation.

[Unity version]
unity2017_1.0f3

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameManager: MonoBehaviour {
    //… omission…
    // Game over processing
    public void GameOver () {
        //… omission…
        Time.timeScale = 0.0f;
        StartCoroutine ("PauseMinites", 2.0f);
        Invoke ("GoBackStageSelect", 2.0f);
        // ↓ If this is added, the scene will not pause.
        // ↓ If deleted, the scene remains stopped.
        Time.timeScale = 1.0f;
    }
    // Return to the stage selection scene
    void GoBackStageSelect () {
        SceneManager.LoadScene ("StageSelectScene");
    }
    // Stop processing for a certain time
    IEnumerator PauseMinites (float sec) {
        Debug.Log ("PauseMinites call");
        yield return new WaitForSeconds (sec);
        Debug.Log ("After Wait, set timeScale to 1.");
        Time.timeScale = 1.0f;
    }
}
  • Answer # 1

    Time.timeScaleis not just a pause, it is"Time flow settings (Unity) in Unity".
    In other words,Time.timeScale = 0.0f;means that the time flow is 0 times = stop the time, so the WaitForSeconds and Invoke time measurement stops.
    (Since the flow of time has stopped, "○ seconds later" will not come)

    Coroutine itself doesn't stop, so
    yield return new WaitForSeconds (sec); for (int i = 0;i<100;i ++) {yield return null;}
    If it is possible, measurement is possible once.
    (yield return null;is the same as waiting for one frame, because the frame is being updated itself)
    AlthoughUpdatedoes not stop, Time.deltaTime is 0, so useTime.realtimeSinceStartupwhen measuring time.

    Things that stop when Unity pauses, things that don't stop 【Time.timeScale】
    Use time not affected by Time.timeScale [Tips]

  • Answer # 2

    As I answered above, I implemented and tried as follows,
    Failed to resume while stopped.

    /// GameManager.cs
    public class GameManager: MonoBehaviour {
        // omission
        // Game over processing
        public void GameOver () {
            textGameOver.SetActive (true);
            PauseMinites (3.0f);
            Invoke ("GoBackStageSelect", 0.0f);
        }
        // Return to the stage selection scene
        public void GoBackStageSelect () {
            SceneManager.LoadScene ("StageSelectScene");
        }
        // Stop processing for a certain time
        void PauseMinites (float sec) {
            Debug.Log ("PauseMinites call");
            Time.timeScale = 0.0f;
            for (lastRealTime = 0.0f, realDeltaTime = 0.0f;sec>realDeltaTime;) {
                Debug.Log ("in realtimeSinceStartup call loop");
                // [? ? ? It seems that we have not escaped from this loop process.
                CalcRealDeltaTime ();
            }
            Time.timeScale = 1.0f;
            Debug.Log ("After Wait, set timeScale to 1.");
        }
        // Calculate elapsed time based on real time
        void CalcRealDeltaTime ()
        {
            if (lastRealTime == 0)
            {
                lastRealTime = Time.realtimeSinceStartup;
            }
            realDeltaTime = Time.realtimeSinceStartup-lastRealTime;
            lastRealTime = Time.realtimeSinceStartup;
        }
    }

    Instead of pausing the entire scene, we changed the implementation policy to stop only the (ball) object you want to stop.
    Specifically, RidgeBody2D's velocity (position movement) and rotation in the Z-axis direction (Rotation) were stopped as follows.

    /// GameManager.cs
    public class GameManager: MonoBehaviour {
        // omission
        // Game over processing
        public void GameOver () {
            textGameOver.SetActive (true);
            Invoke ("GoBackStageSelect", 3.0f);
        }
        // Return to the stage selection scene
        public void GoBackStageSelect () {
            SceneManager.LoadScene ("StageSelectScene");
        }
    }
    /// PlayerManager.cs
    public class PlayerManager: MonoBehaviour {
        private Rigidbody2D rbody;// rigidbody2D for player control
        void Start () {
            rbody = GetComponent<Rigidbody2D>();
        }
        // collision handling
        void OnTriggerEnter2D (Collider2D col)
        {
            // In case of collision with a stopped Bomb or out of range
            if ("Bomb" .Equals (col.gameObject.tag) || "Trap" .Equals (col.gameObject.tag))
            {
                // ball \ stop processing
                StopPlayer ();
                gameManager.GetComponent<GameManager>(). GameOver ();
            }
        }
        // Ball movement/rotation stop and gravity invalidation processing
        void StopPlayer () {
            rbody.velocity = Vector2.zero;
            rbody.freezeRotation = true;
            rbody.isKinematic = true;
        }
    }

Related articles