Help with script - Pausing game while read a note

Hi.

I´m trying to edit a script, wich allows the player to read a note upon pressing E. I made some changes, but i have a question: There´s a way to pause the game when player hit “E” to read the note, and unpause when hit “E” again?

Sorry for my bad english, i´m from Brazil.

Here´s the script i´m usging. Is attached to the player.

  • publicfloat maxDistance =1.5F;
  • publicGUISkin skin;
  • privatebool readingNote =false;
  • publicAudioClipNoteOpen;
  • publicAudioClipNoteClose;
  • voidStart(){
  • StartCoroutine(CheckForInput());
  • }
  • privateIEnumeratorCheckForInput(){
  • if(Input.GetKeyDown(KeyCode.F)){
  • if(!readingNote)
  • CheckForNote();
  • else{
  • readingNote =false;
  • audio.PlayOneShot(NoteClose);
  • }
  • }
  • yieldreturnnull;
  • }
  • }
  • privatevoidCheckForNote(){
  • Ray ray =Camera.main.ViewportPointToRay(newVector3(0.5F,0.5F,0));
  • RaycastHit data;
  • if(Physics.Raycast(ray,out data, maxDistance)){
  • if(data.transform.name ==“Note”){
  • noteText = data.transform.GetComponent().Text;
  • readingNote =true;
  • audio.PlayOneShot(NoteOpen);
  • }
  • }
  • }
  • voidOnGUI(){
  • if(skin)
  • GUI.skin = skin;
  • if(readingNote){
  • GUI.Box(newRect(Screen.width /4F,Screen.height /16F,Screen.width /2F,Screen.height *0.75F), noteText);
  • }
  • }

Hey first of all I’d like to point you to the “Using code tags properly” thread which shows you how to nicely format code here in the boards:

About the problem - the usual way to do any pausing would be to set the Time.timeScale to a very low factor like 0.000001f. Don’t set it to 0 though since that has turned out to break stuff here and there. The low value is just as good.

When E is hit again, just set it back to 1. Something like this which will toggle the timeScale:

Time.timeScale != 1f ? 1f : 0.000001f;

Hello.
I forgot to say I have no skills in C#. Could you please show me where to insert this lines in the script?

Just put Time.timeScale = 0.000001f next to your “readingNote = true;” statement, meaning once you set to read a note. Because that’s where you actually want to pause the game, don’t you?

Reset the Time.timeScale back to 1 next to setting “readingNote = false;”

Works!!! Thanks for your help, Glockenbeat!