Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more..
    Dismiss Notice
  3. Dismiss Notice

Can't get objects to stop moving after Pausing

Discussion in 'Scripting' started by dyach3579, Apr 14, 2020.

  1. dyach3579

    dyach3579

    Joined:
    Jan 3, 2018
    Posts:
    22
    Hi I'm creating a Mobile endless game. I have some code in my GameManager script for a Pause button. I have a pause menu that comes up. The menu comes up just fine, and it will pause the game, but if an obstacle was already spawned before I hit the Pause button then that obstacle keeps moving even when I paused it and the menu appears. Any ideas on how to prevent the obstacles from still moving? I can't find this anywhere online for Mobile. Just want to pause everything basically.

    I didn't post my entire GameManager code, just the part where i coded the Pause button. Thank you for any help.

    Code (CSharp):
    1.  
    2. public GameObject pausePanel;
    3. public static bool GameIsPaused = false;
    4.  
    5. public void Resume()
    6.     {
    7.         pausePanel.SetActive(false);
    8.         Time.timeScale = 1f;
    9.         GameIsPaused = false;
    10.     }
    11.  
    12.     public void Pause()
    13.     {
    14.         pausePanel.SetActive(true);
    15.         Time.timeScale = 0f;
    16.         GameIsPaused = true;
    17.     }
    18.  
    19.     public void LoadMenu()
    20.     {
    21.         Time.timeScale = 1f;
    22.         SceneManager.LoadScene("MainMenu");
    23.  
    24.         if (instance == null)
    25.         {
          
    26.            instance = this;
          
    27.            DontDestroyOnLoad(gameObject);
      
    28.          }
      
    29.          else
      
    30.          {
          
    31.             Destroy(gameObject);
      
    32.          }
    33.     }
    34.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    1. make sure your objects move according to Time.deltaTime. If not, they'll continue to move

    2. make sure your Pause function is actually being called:
    - make a Debug.Log() call inside it
    - display the Time.timeScale in the log during your Update() loop to make sure someone else isn't setting it back to 1
     
    Joe-Censored and PraetorBlue like this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    It would be helpful to see the code that is controlling the movement of your obstacles, but yeah pretty much what Kurt said. If you are not incorporating either Time.deltaTime or Time.time into the calculations for the motion of your obstacles, they won't care at all about what the value of Time.timeScale is.
     
    Kurt-Dekker and Joe-Censored like this.
  4. dyach3579

    dyach3579

    Joined:
    Jan 3, 2018
    Posts:
    22
    Hi thank you for responding. Here is my obstacle movement code

    Code (CSharp):
    1. public class ObstacleMovement : MonoBehaviour {

    
    2.  
    3. [SerializeField] float moveSpeed;

    
    4. public int ColorID;
    
    5. public bool isEnemy = false;
    
    6. public float yThreshhold = 10f;
    7.  
    8. [HideInInspector]
    public bool isDying = false;
    9.  
    10. 
     private void Awake()
    
    11.      {
          
    12.          isDying = false;
        
    13.          moveSpeed = Random.Range(0.04f, 0.04f);       
    
    14.      } 


    
    15.  
    16.    void Update()
    
    17.     {
        
    18.            Vector3 currentpos = transform.position;
        
    19.            currentpos.y += moveSpeed; // * Time.deltaTime;      
    20.            transform.position = currentpos;
    21.  
    22. 
            if (transform.position.y > yThreshhold || !GameManager.instance.bGameStarted) 
        
    23.             {
                      
    24.                 Destroy(transform.gameObject);
        
    25.             }
    
    26.       }

    
    27.  
    28.     private void FixedUpdate()
    
    29.     {
    30.      }
    31. }
    32.  
    When I uncomment the *Time.deltaTime in the Update function, the obstacles don't move at all.
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    They are moving, just really slowly. Your movement speed is set as .04 units per second. Try increasing the movementSpeed.
     
  6. dyach3579

    dyach3579

    Joined:
    Jan 3, 2018
    Posts:
    22
    Nice. You’re absolutely right. I bumped up the speed a little bit and didn’t do much but now I bumped it up to 3f and they moved just fine. My bad. Thank you again for your help.
     
  7. dyach3579

    dyach3579

    Joined:
    Jan 3, 2018
    Posts:
    22
    Thank you for your help! Works good now.
     
    Kurt-Dekker likes this.