Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

I can't unfreeze Rigidbody2D position

Discussion in 'Editor & General Support' started by csviktor, Jan 23, 2021.

  1. csviktor

    csviktor

    Joined:
    Jan 2, 2021
    Posts:
    5
    I'm working on a coin game where coins are falling down from the sky. I want to freeze the coin position when the user is launching the quit menu and return to game if he is returning to game.

    I'm using this code in coin's Update method:

    private void Start()
    {
    Debug.Log(gameObject.name);
    rb = GetComponent<Rigidbody2D>();

    }

    void Update()
    {
    //transform.Rotate(0, rotateSpeed, 0, Space.World);
    if (GameManager.isGamePaused)
    {
    rb.constraints = RigidbodyConstraints2D.FreezePosition;
    } else
    {
    rb.constraints = RigidbodyConstraints2D.None;
    }
    }

    But when I'm testing the game, the current coin freezes during quit menu. But when I'm returning the game, the coin is not falling downward and the spawner soon created the next coin. How can I unfreeze the freezed coin?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,522
    Freezing the position will also remove the object's velocity as it slams to a stop.

    A better way to pause is to set
    Time.timeScale = 0.0f;
    , then restore it to 1.0f when you want to continue.
     
    csviktor likes this.
  3. csviktor

    csviktor

    Joined:
    Jan 2, 2021
    Posts:
    5
    Thank you, so this is the ultimate "pause game" solution. It works!
     
    Kurt-Dekker likes this.