Pause Movement

when i use time.timescale = 0; its ok and game is paused , but my player which with following codes moves does not , i mean player regardless of pause still continues to move, any soluton ? thanks .

void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
           
            mouseDownPos = Input.mousePosition;
        }

        if (Input.GetMouseButtonUp (0))
        {
//            if(!EventSystem.current.IsPointerOverGameObject())
//            {

            if (Input.mousePosition.y > mouseDownPos.y) {
                float y = player.transform.position.y;

                if (y == 0.5) {
                    player.transform.position = new Vector2 (-4, 1.5f);
                }
                if (y == -0.5) {
                    player.transform.position = new Vector2 (-4, 0.5f);
                    sprite = GetComponent<SpriteRenderer> ();
                    sprite.sortingOrder = 1;
                }
                if (y == -1.5) {
                    player.transform.position = new Vector2 (-4, -0.5f);
                }
                   
            } else if (Input.mousePosition.y < mouseDownPos.y)
                {
                float y = player.transform.position.y;
                if (y == 1.5) {
                    player.transform.position = new Vector2 (-4, 0.5f);
                }
                if (y == 0.5) {
                    player.transform.position = new Vector2 (-4, -0.5f);
                }
                if (y == -0.5) {
                    player.transform.position = new Vector2 (-4, -1.5f);
                    sprite = GetComponent<SpriteRenderer> ();
                    sprite.sortingOrder = 3;
                }
//                }
            }
        }
    }

If you have a pause button, just have a variable, probably a static bool somewhere
public static bool isPaused; for example

And when your game is pause, you set that value to true, then check if that value is true, if so, don’t move. If false, move.
Can always check this at the start of your update and if it’s true, just do a return to exit out of the update.

If your player is in the process of moving and the game is paused and he keeps moving, you may need to add additional logic to pause and then continue movement, but that shouldn’t be to bad to do.

1 Like

Brathnann has posted a good solution, the reason this is happening is because your update function is still continuously checking for key presses even while all your other stuff has been paused due to timescale so you need to just find a way to disable the movement manually each time you pause the game as he’s said.

Allright , in same direction , would you please explain , how i can reference to Update function from playermovement script in another class and from that class by using a bool make it , is it a better way?

I think you’re asking how to get a variable in another script? In this case, we’re using a static variable to make it easier and accessible from any other script without worrying about creating a reference. Where you put this variable is sort of up to you and how your game is designed. But, as purely an example.

public class GameManager: MonoBehaviour
{
    public static bool isPaused;

    //Other code stuff
}

You can modify it with

GameManager.isPaused = true;

and check it

if(GameManager.isPaused)
{
   return; //So we don't run the rest of the update code, for example.
}

i used like below , it works , please tell me if it is ok or not :

using UnityEngine;
using System.Collections;

public class PlayerAnimationController : MonoBehaviour {

    private Animator anim;
    public GameObject GameoverPanel;
    public GameObject Buttons;
    public PlayerMovement pm;

    void Awake()
    {
        anim = GetComponent<Animator> ();

    }
    void Update()
    {


    }
     void OnCollisionEnter2D(Collision2D col)
    {
        if (col.gameObject.tag == "Enemy")
        {
            StartCoroutine (retro ());

        }
    }

    IEnumerator retro()
    {
        transform.localPosition = new Vector3(-4, -0.5f, 0);
        var running = false;
        anim.SetBool ("Running", running);
        yield return new WaitForSeconds(1f);
        Time.timeScale = 0;
        gameoverPanel ();
        pm.isPaused = true;

    }
    public void gameoverPanel()
    {
        GameoverPanel.SetActive (true);
        Buttons.SetActive (false);

    }
}
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;


public class PlayerMovement : MonoBehaviour
{
    private Vector3 mouseDownPos;

    public GameObject player;
    private SpriteRenderer sprite;
    public bool isPaused = true;

    void Start()
    {

    }
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
           
            mouseDownPos = Input.mousePosition;
        }

        if (Input.GetMouseButtonUp (0))
        {
            if(!isPaused)
            {

            if (Input.mousePosition.y > mouseDownPos.y) {
                float y = player.transform.position.y;

                if (y == 0.5) {
                    player.transform.position = new Vector2 (-4, 1.5f);
                }
                if (y == -0.5) {
                    player.transform.position = new Vector2 (-4, 0.5f);
                    sprite = GetComponent<SpriteRenderer> ();
                    sprite.sortingOrder = 1;
                }
                if (y == -1.5) {
                    player.transform.position = new Vector2 (-4, -0.5f);
                }
                   
            } else if (Input.mousePosition.y < mouseDownPos.y)
                {
                float y = player.transform.position.y;
                if (y == 1.5) {
                    player.transform.position = new Vector2 (-4, 0.5f);
                }
                if (y == 0.5) {
                    player.transform.position = new Vector2 (-4, -0.5f);
                }
                if (y == -0.5) {
                    player.transform.position = new Vector2 (-4, -1.5f);
                    sprite = GetComponent<SpriteRenderer> ();
                    sprite.sortingOrder = 3;
                }
                }
            }
        }
    }

}

the only problem is i expect player always remain on its local position , but it is moved to left , how i can make it ok ?