I got problem with pause menu holding esc key fix.
I writed simple script that blocks showing/hiding pause menu for 5 seconds, but it doesn't work.
Can someone tell me what I'm doing wrong or fix my code?
(Here I'm writing cuz i cant post my question lol)
Here is code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MenuController : MonoBehaviour
{
public static bool IsGamePaused = false;
public static bool IsClickingBlocked = false;
public GameObject zagadka1;
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.T))
{
if (IsGamePaused)
{
if (IsClickingBlocked == false) {
ClickStopCounter();
zagadka1Start();
}
}
else
{
if (IsClickingBlocked == false)
{
zagadka1Stop();
ClickStopCounter();
}
}
}
}
IEnumerator ClickStopCounter()
{
IsClickingBlocked = true;
yield return new WaitForSeconds(5);
IsClickingBlocked = false;
}
public void zagadka1Start()
{
zagadka1.SetActive(false);
Time.timeScale = 1f;
IsGamePaused = false;
Cursor.lockState = CursorLockMode.Locked;
}
void zagadka1Stop()
{
zagadka1.SetActive(true);
Time.timeScale = 0f;
IsGamePaused = true;
Cursor.lockState = CursorLockMode.None;
}
You should use StartCoroutine when calling the Coroutine.
Try this one. StartCoroutine(ClickStopCounter())
Related
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PauseMenu : MonoBehaviour
{
public static bool gameIsPaused = false;
public GameObject PauseMenuUI;
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (gameIsPaused)
{
Resume();
} else
{
Pause();
}
}
Debug.Log("KeyInput");
}
public void Resume ()
{
PauseMenuUI.SetActive(false);
Time.timeScale = 1f;
gameIsPaused = false;
}
void Pause ()
{
PauseMenuUI.SetActive(true);
Time.timeScale = 0f;
gameIsPaused = true;
}
public void LoadMenu()
{
Debug.Log("Loading Menu...");
Time.timeScale = 1f;
SceneManager.LoadScene ("MainMenu");
}
public void QuitGame()
{
Debug.Log ("Quitting Game");
Application.Quit();
}
}
My code wont open the actual pause screen is there any reason why
i have tried editing it in different ways and i also tried some other code online but it never seems to work for me i have also went onto the unity forms and that wasn't really helpful
im trying to follow this video
and i think i did everything right but its not doing anything then i click the escape button
using System.Collections.Generic;
using UnityEngine;
public class PauseMenu : MonoBehaviour
{
public static bool GameIsPaused = false;
public GameObject pauseMenuUI;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (GameIsPaused)
{
Resume();
}else
{
Pause();
}
}
}
public void Resume()
{
pauseMenuUI.SetActive(false);
Time.timeScale = 1f;
GameIsPaused = false;
}
void Pause()
{
pauseMenuUI.SetActive(true);
Time.timeScale = 0f;
GameIsPaused = true;
}
public void LoadMenu()
{
Debug.Log("loading menu");
}
public void QuitGame()
{
Debug.Log("quiting game");
}
}
this is how the panel looks like
it was before in Canvas but i moved it because i was trying to make it work but it didnt help
someone knows maybe how to fix it?
I have a code that manages my Pause Menu window of my scene.
It works well, but similar to the "Audiolistenner.pause=true", I would also like to lock the camera view when the pause window is activated.
How would I adjust this code? Is it the Screen.lockCursor function? If so, how to implement it?
Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PauseMenu : MonoBehaviour
{
[SerializeField] private GameObject pauseMenuUI;
[SerializeField] private bool isPaused;
private void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
isPaused = !isPaused;
}
if (isPaused)
{
ActivateMenu();
}
else
{
DeactivateMenu();
}
}
void ActivateMenu()
{
Time.timeScale = 0;
AudioListener.pause = true;
pauseMenuUI.SetActive(true);
}
public void DeactivateMenu()
{
Time.timeScale = 1;
AudioListener.pause = false;
pauseMenuUI.SetActive(false);
isPaused = false;
}
}
Thanks in advance!
When the pause function start:
Vector3 lockedCamPosition = camera.transform.position;
Vector3 lockedCamRotation = camera.transform.rotation;
The somewhere in the update function:
camera.transform.position = lockedCamPosition;
camera.transform.rotation = Quarintine.Euler(lockedCamRotation );
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SaveGameMessage : MonoBehaviour
{
public Text text;
public float speed;
public bool startFading = false;
public bool enableText = false;
private Color textColor;
private void Start()
{
text = GetComponent<Text>();
textColor = text.color;
}
private void Update()
{
if (startFading == true)
{
if (enableText == true)
{
text.enabled = true;
}
textColor.a = (Mathf.Sin(Time.time * speed) + 1.0f) / 2.0f;
text.color = textColor;
}
else
{
text.enabled = false;
}
}
}
And using it :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class PlayingInGameScenesController : MonoBehaviour
{
public CinemachineFreeLook[] freeLookcameras;
public LockController lockController;
public GameObject uiSceneText;
public GameObject player;
public float transitionSpeed = 5f;
public float thresHold = 0.1f;
public bool talkingProcess = true;
public SaveGameMessage saveGameMessage;
private bool newGame = true;
private void Start()
{
}
public void PlayingSceneInGame()
{
PlayingSceneStatesControls(true);
StartCoroutine(ScenePlayingTime());
}
private void Update()
{
if (SceneManager.GetActiveScene().name != "Main Menu" && newGame == true)
{
PlayingSceneInGame();
newGame = false;
}
}
private void LateUpdate()
{
}
private void PlayingSceneStatesControls(bool LockState)
{
lockController.LockControl(LockState);
if (LockState == true)
{
uiSceneText.SetActive(true);
}
else
{
talkingProcess = false;
uiSceneText.SetActive(false);
}
}
IEnumerator ScenePlayingTime()
{
yield return new WaitForSeconds(10);
PlayingSceneStatesControls(false);
freeLookcameras[0].enabled = false;
freeLookcameras[1].enabled = true;
var brain = Camera.main.GetComponent<CinemachineBrain>().m_DefaultBlend.m_Time;
saveGameMessage.enableText = true;
saveGameMessage.startFading = true;
player.GetComponent<Player>().SavePlayer();
StartCoroutine(FakeSave());
}
IEnumerator FakeSave()
{
yield return new WaitForSeconds(7);
saveGameMessage.startFading = false;
}
}
At the bottom I'm making fake for 7 seconds the problem is when the fake finish the ui text enable false at once. In the SaveGameMessage script at the bottom I'm doing :
text.enabled = false;
but that is ugly it's just stop the fade in out effect at once. I want somehow to enable it false but before that to finish the current fading so the text enabled false will be smooth fading out and ot just suddenly stopping the fading effect.
Before you disable it run an animation to fade it out. this animation should first be made or maybe you can find one online.
then just run the animation before setting it to false.
Whenever I pause everything stops, that's how I want it to be, but the only thing that does not stop is the camera. I move my cursor the camera still follows. I want it to freeze. I am using the FPS Controller Asset in the asset store
using System.Collections;
using System.Collections.Generic;
using UnityStandardAssets.Characters.FirstPerson;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PauseMenu : MonoBehaviour
{
public static bool GameIsPaused = false;
static bool IsPaused = false;
public GameObject pauseMenuUI;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (GameIsPaused)
{
Resume();
} else
{
Pause();
}
}
}
public void Resume()
{
pauseMenuUI.SetActive(false);
Time.timeScale = 1f;
AudioListener.pause = false;
GameIsPaused = false;
}
void Pause()
{
pauseMenuUI.SetActive(true);
Time.timeScale = 0f;
AudioListener.pause = true;
GameIsPaused = true;
}
public void LoadMenu()
{
Time.timeScale = 1f;
SceneManager.LoadScene("Menu");
}
public void QuitGame()
{
Debug.Log("Exiting Game");
Application.Quit();
}
}
Get the reference to the Character Controller Script and then disable it:
mycharfpscon.enabled = false;
And then enabble it back:
mycharfpscon.enabled = true;