S
S
SkyCrusher2018-11-28 12:16:37
C++ / C#
SkyCrusher, 2018-11-28 12:16:37

How to make a delay after a pause?

The problem is this. There is a pause that stops the whole game through Time.timeScale, and the animation is played. Everything works fine. Only the player, after removing the pause, does not have time to react and, most often, dies. In connection with this, there is a question. How to make it so that after you resume the game, the player is given a certain amount of time (for example, five seconds), which is displayed (!) On the screen. By type, I pressed the pause, the countdown on the screen (5, 4, 3, 2, 1) and the game resumed. I googled for a long time, I could not find anything sensible in this regard. I hope for your help.
Here is the script through which I want to implement this whole bath.

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

public class PauseButton : MonoBehaviour {

    bool OnClick;
    public static bool Paused;

    public float WaitToPause;
    public Sprite SpritePause;
    public Sprite SpritePlay;
    Text TimeAfterPause;

    private void Start()
    {
        GetComponent<Image>().sprite = SpritePause;
        Paused = false;
    }

    public void Pause()
    {
        if (!Paused && !OnClick)
        {
            OnClick = true;
            GetComponent<Image>().sprite = SpritePlay;
            Paused = true;
            StartCoroutine(AnimationStop(new Vector3(180, 0, 0), 0.3f));
        }
        else if (Paused && !OnClick)
        {
            Time.timeScale = 1;
            OnClick = true;
            GetComponent<Image>().sprite = SpritePause;
            Paused = false;
            StartCoroutine(AnimationPlay(new Vector3(180, 0, 0), 0.3f));
        }
    }

    IEnumerator AnimationStop(Vector3 byAngles, float inTime)
    {
        var fromAngle = transform.rotation;
        var toAngle = Quaternion.Euler(transform.eulerAngles + byAngles);
        for (var t = 0f; t < 1; t += Time.deltaTime / inTime)
        {
            transform.rotation = Quaternion.Lerp(fromAngle, toAngle, t);
            yield return null;
        }
        OnClick = false;
        StartCoroutine(Stop());
    }

    IEnumerator AnimationPlay(Vector3 byAngles, float inTime)
    {
        var fromAngle = transform.rotation;
        var toAngle = Quaternion.Euler(transform.eulerAngles + byAngles);
        for (var t = 0f; t < 1; t += Time.deltaTime / inTime)
        {
            transform.rotation = Quaternion.Lerp(fromAngle, toAngle, t);
            yield return null;
        }
        OnClick = false;
    }

    IEnumerator Stop()
    {
        Time.timeScale = 0;
        yield return null;
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Kikhno, 2018-11-29
@SkyCrusher

To solve this problem, I suggest using Coroutine.
Each iteration holds a second, up to a certain timerAfterPause time limit.
Pressing the "A" key will start the countdown.

private const int timerAfterPause = 5;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.A))
            StartCoroutine(StartWaitAfterPause(timerAfterPause));
    }

    IEnumerator StartWaitAfterPause(int value)
    {      
        int iterator = 0;

        while (iterator < value)
        {
            Debug.Log(value - iterator);
            yield return new WaitForSeconds(1f);
            iterator++;
        }
        EndWaitAfterPause();
    }

    void EndWaitAfterPause()
    {
        StopCoroutine("WaitAfterPause");
        Debug.Log("GO!");
    }

V
Vladimir S, 2018-11-28
@hePPer

so you do not press him immediately pause. the player pressed the unpause button - you display the countdown for him and then shoot.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question