1

So I am currently trying to make a game and I want one of the features to be when you hit a certain block I called it Obstacle you will pause for 1 second. I just don't know how to add that pause in C#. The script:

using UnityEngine;

public class PlayerCollision : MonoBehaviour {
    public Movememt movement;
         
    void OnCollisionEnter (Collision Collisioninfo)
    {           
       if (Collisioninfo.collider.name == "Obstacle")
        {
            movement.enabled = false;
           // i want the pause here
            movement.enabled = true;
            
        }
    }
}
eglease
  • 2,445
  • 11
  • 18
  • 28
Max
  • 57
  • 4
  • 4
    Please include code as text, not as a screenshot of code – Jamiec Jan 18 '22 at 11:34
  • I don't know Unity or Unity scripting but Unity runtime might have a timer mechanism, so create a timer and start that timer when you hit the obstacle, when the required time passed, renable the player state. – jtxkopt Jan 18 '22 at 14:55

3 Answers3

2

You can do it using coroutines.

You can change the return value from void to IEnumerator which will allow you to "pause" by yeilding a new WaitForSeconds instance. Here is an example:

IEnumerator OnCollisionEnter(Collision collision)
{           
    if (collision.collider.name == "Obstacle")
    {
        movement.enabled = false;
        yield return new WaitForSeconds(1);
        movement.enabled = true;
    }
}
Vapid
  • 701
  • 7
  • 27
0

You can use WaitForSeconds with coroutines. For details please go through the link.

yield return new WaitForSeconds(1);

Dharman
  • 30,962
  • 25
  • 85
  • 135
mohammadAli
  • 375
  • 2
  • 13
-2

You can pause or resume the game by setting the timescale to 0 or back to 1.

void PauseGame()
    {
        Time.timeScale = 0;
    }

void ResumeGame()
    {
        Time.timeScale = 1;
    }