1
\$\begingroup\$

I have already a running script of countdown and it works fine , the problem is that I want to pause or delay the game while the countdown is displaying, the moment countdown finished the player is already dead. Here is my code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;


public class jb : MonoBehaviour {
    private string countdown = "";    
    private bool showCountdown = false;
    public AudioSource noise1;
    public AudioSource noise2; 
    public Sprite sprite2;
    public Vector2 jump = new Vector2 (0,300);
    private SpriteRenderer spriteRenderer; 
    void Start () {
        //GetComponent<Rigidbody2D>().velocity = Vector2.right * speed;
        StartCoroutine(getReady());

        spriteRenderer = GetComponent<SpriteRenderer>();
        noise1.GetComponent<AudioSource>().Play();

    }

    // Update is called once per frame
    void Update () {

        if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
        { 
           Vector2 touchPosition = Input.GetTouch(0).position;
            GetComponent<Rigidbody2D>().AddForce(jump);
            //noise1.GetComponent<AudioSource>().Play();
        }

        Vector2 screen_position = Camera.main.WorldToScreenPoint (transform.position);
        if(screen_position.y > Screen.height || screen_position.y < 0)
        {
            noise1.GetComponent<AudioSource>().Stop();
            noise2.GetComponent<AudioSource>().Play();
            StartCoroutine(Die());
        }

    }




    void OnCollisionEnter2D(Collision2D det)
    {
        noise1.GetComponent<AudioSource>().Stop();
        noise2.GetComponent<AudioSource>().Play();
        StartCoroutine(Die());

    }



    IEnumerator Die(){
        PlayerPrefs.SetString( "previousLevel", SceneManager.GetActiveScene().name );
        spriteRenderer.sprite = sprite2;
        yield return new WaitForSeconds (2);
        SceneManager.LoadScene("score");
     }


    IEnumerator getReady()    
    {
        showCountdown = true;    

        countdown = "3";    
        yield return new WaitForSeconds (1);  

        countdown = "2";    
        yield return new WaitForSeconds (1);

        countdown = "1";    
        yield return new WaitForSeconds (1);

        countdown = "GO";    
        yield return new WaitForSeconds (1);

        showCountdown = true;
        countdown = "";  
    }

    void OnGUI ()
    {
        if (showCountdown)
        {    
            GUI.color = Color.red;    
            GUI.Box ( new Rect (Screen.width / 2 - 100, 50, 200, 175), "GET READY");

            // display countdown    
            GUI.color = Color.white;    
            GUI.Box (new Rect (Screen.width / 2 - 90, 75, 180, 140), countdown);
        }    
    }
}

what can I do to pause the game? I tried invoke (by creating another function and copying everything over there) in update call but it didn't work.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

I've added a simple boolean value to your code: started.

At the start, it's false so Update() doesn't update the game. When the countdown finishes, we set it to true so Update() can update.

Also, you need to make your player's Rigidbody2D unable to move by making it kinematic in the Start() method:

GetComponent<Rigidbody2D>().isKinematic = true;

and then set it to false after the countdown :)

public class jb : MonoBehaviour {
    private string countdown = "";    
    private bool showCountdown = false;
    public AudioSource noise1;
    public AudioSource noise2; 
    public Sprite sprite2;
    public Vector2 jump = new Vector2 (0,300);
    private SpriteRenderer spriteRenderer; 


    bool started = false;


    void Start () {
        //GetComponent<Rigidbody2D>().velocity = Vector2.right * speed;
        StartCoroutine(getReady());

        spriteRenderer = GetComponent<SpriteRenderer>();
        noise1.GetComponent<AudioSource>().Play();

        GetComponent<Rigidbody2D>().isKinematic = true;
    }

    // Update is called once per frame
    void Update () {

        if(!started) return;

        if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
        { 
           Vector2 touchPosition = Input.GetTouch(0).position;
            GetComponent<Rigidbody2D>().AddForce(jump);
            //noise1.GetComponent<AudioSource>().Play();
        }

        Vector2 screen_position = Camera.main.WorldToScreenPoint (transform.position);
        if(screen_position.y > Screen.height || screen_position.y < 0)
        {
            noise1.GetComponent<AudioSource>().Stop();
            noise2.GetComponent<AudioSource>().Play();
            StartCoroutine(Die());
        }

    }




    void OnCollisionEnter2D(Collision2D det)
    {
        noise1.GetComponent<AudioSource>().Stop();
        noise2.GetComponent<AudioSource>().Play();
        StartCoroutine(Die());

    }



    IEnumerator Die(){
        PlayerPrefs.SetString( "previousLevel", SceneManager.GetActiveScene().name );
        spriteRenderer.sprite = sprite2;
        yield return new WaitForSeconds (2);
        SceneManager.LoadScene("score");
     }


    IEnumerator getReady()    
    {
        showCountdown = true;    

        countdown = "3";    
        yield return new WaitForSeconds (1);  

        countdown = "2";    
        yield return new WaitForSeconds (1);

        countdown = "1";    
        yield return new WaitForSeconds (1);

        countdown = "GO";    
        yield return new WaitForSeconds (1);

        showCountdown = true;
        countdown = "";  

        started = true;
        GetComponent<Rigidbody2D>().isKinematic = false;
    }

    void OnGUI ()
    {
        if (showCountdown)
        {    
            GUI.color = Color.red;    
            GUI.Box ( new Rect (Screen.width / 2 - 100, 50, 200, 175), "GET READY");

            // display countdown    
            GUI.color = Color.white;    
            GUI.Box (new Rect (Screen.width / 2 - 90, 75, 180, 140), countdown);
        }    
    }
}
\$\endgroup\$
6
  • \$\begingroup\$ Nope it's not working :( \$\endgroup\$
    – melissa
    Commented Sep 22, 2017 at 13:07
  • \$\begingroup\$ Please explain what's happenning in-game. \$\endgroup\$
    – Jacob
    Commented Sep 22, 2017 at 13:08
  • \$\begingroup\$ It;s flappy bird sort of game, a bit altered but the basic idea is same, user touch screen to keep the player in the air. So when I play the click button on main menu I want to show the countdown first with the game behind the countdown box paused but right now when I press the play button there is a countdown box infront and behind the player falls down (because the game is not paused \$\endgroup\$
    – melissa
    Commented Sep 22, 2017 at 13:15
  • \$\begingroup\$ Aw, I get it. You have a Rigidbody on the player! :) Let me edit the code. \$\endgroup\$
    – Jacob
    Commented Sep 22, 2017 at 13:25
  • \$\begingroup\$ Check it out now! \$\endgroup\$
    – Jacob
    Commented Sep 22, 2017 at 13:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .