PauseMenu - FPS Camera still moves when in PauseMenu - c#

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;

Related

Why wont my Pause screen come up when i click esc (in Unity)

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

i cant make this pause menu work in unity

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?

How do I lock my camera view when Pause Menu window activated in Unity scene?

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 );

How can I access a variable dynamically in a script not attached to the gameobject itself in unity?

I have several gameobjects: each have an "Interactable" and "Data" class. I also have a "PauseGameController" class not attached to those gameobjects in which I want to use the data for each object dynamically if an event is triggered when the Key I is pressed AND the variable "isInteracting" from the "Interactable" class is true. In other words , the "PauseGameController" class does not know which gameobject data will be using until this 2 events happend for one specific object
My question is:
How can I retrieve the data dynamically that I typed on the inspector in the class "Data" for each gameobject from another script not attached to this gameobject?
I want to re-use the "data" and "interactable" class and be able to set different data to different gameobjects as I am creating them in the scene. And I don't want to use the Finds functions because the documentation says that it's slow.
Code:
Interactables class
using UnityEngine;
using System.Collections;
public class Interactables : MonoBehaviour {
public static bool interactable = false;
public bool interactableown = false;
public Material[] material;
Renderer rend;
// Use this for initialization
void Start () {
rend = GetComponent<Renderer>();
rend.enabled = true;
rend.sharedMaterial = material[0];
}
// Update is called once per frame
void Update () {
if (interactableown)
{
rend.sharedMaterial = material[1];
}else
{
rend.sharedMaterial = material[0];
}
}
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "Player")
{
interactable = true;
interactableown = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Player")
{
interactable = false;
interactableown = false;
}
}
}
Pause Class
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Puase : MonoBehaviour {
public static bool isPause = false;
public GameObject MenuUI;
public Text txt;
// Update is called once per frame
void Update () {
if (Input.GetKeyUp(KeyCode.I) && Interactables.interactable)
{
if (isPause)
{
Resume();
}
else
{
Pause();
}
}
}
public void Resume()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
MenuUI.SetActive(false);
Time.timeScale = 1f;
isPause = false;
}
public void Pause()
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
MenuUI.SetActive(true);
Time.timeScale = 0f;
isPause = true;
}
}
Data Class
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DataObj : MonoBehaviour {
public string info = "";
void Start () {
}
// Update is called once per frame
void Update () {
}
}
Capture of the editor
Capture

Don't know how to use SetActive() properly

So, my script that I've written isn't working fully. I have A pause button and when I press it, it triggers my bool and shows that its working properly but when I'm "Paused" my UI doesn't pop up and my game doesn't stop in the back ground. I hope you can understand clearly. I am a beginner!
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PauseManager : MonoBehaviour {
public GameObject pauseMenu;
public bool paused = false;
public void start()
{
pauseMenu.SetActive(false);
}
public void update()
{
if(Input.GetButtonDown("escape"))
{
paused = !paused;
}
if (paused)
{
pauseMenu.SetActive(true);
Time.timeScale = 0;
}
if (!paused)
{
pauseMenu.SetActive(false);
Time.timeScale = 1;
}
}
public void Resume()
{
paused = false;
}
public void pauseButton()
{
paused = true;
}
}
Advice you change your code to this:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PauseManager : MonoBehaviour {
public GameObject pauseMenu;
bool paused = false;
void Start()
{
pauseMenu.SetActive(false);
}
void Update()
{
if(Input.GetButtonDown("escape"))
{
paused = !paused;
}
if (paused)
{
PauseGame();
}
if (!paused)
{
ResumeGame();
}
}
void PauseGame(){
pauseMenu.SetActive(true);
Time.timeScale = 0f;
}
void ResumeGame(){
pauseMenu.SetActive(false);
Time.timeScale = 1f;
}
public void Resume()
{
ResumeGame();
}
public void pauseButton()
{
PauseGame();
}
}
I think the problem is on that when you pause button pressed, timescale be 0, and update does not work.

Categories

Resources