- Home /
I legit just figured it out lol
Mouse no do what me need do
I have a script called "MouseBoi" because haha funny, and it controls my caveman style 3D camera, and I have a script called "PauseMenu", and when you hit tab, its supposed to activate a panel and stop time and blah blah blah for a pause menu, only problem is, it doesn't 100% show the mouse, instead, the mouse flickers very randomly. Scripts are below, and if this has already been asked/answered, then oop, my bad
MouseBoi
{
public Transform playerBody;
public float mouseSensitivity = 400f;
float xRotation = 0f;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
xRotation = 30;
xRotation = Mathf.Clamp(xRotation, -90f, 90);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
}
PauseMenu
{
public static bool GameIsPaused = false;
public GameObject pauseMenuUI;
void Update()
{
if (Input.GetKeyDown(KeyCode.Tab))
{
if (GameIsPaused)
{
Resume();
}
else
{
Pause();
}
}
if (GameIsPaused == true)
{
GameObject.Find("Camera.main").GetComponent<MouseBoi>().enabled = false;
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
if (GameIsPaused == false)
{
GameObject.Find("Camera.main").GetComponent<MouseBoi>().enabled = false;
Cursor.lockState = CursorLockMode.None;
Cursor.visible = false;
}
}
void Resume()
{
pauseMenuUI.SetActive(false);
Time.timeScale = 1f;
GameIsPaused = false;
}
void Pause ()
{
pauseMenuUI.SetActive(true);
Time.timeScale = 0f;
GameIsPaused = true;
}
}
Help is greatly apricated in any way, shape, or form.
from what ou describe i'd assume that you set the mouse visibility state in some other script as well. Can you check i that is the case?
Other then that id suggest some simplifications for your code. If you have something like
if (GameIsPaused == true)
{
GameObject.Find("Camera.main").GetComponent<MouseBoi>().enabled = false;
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
if (GameIsPaused == false)
{
GameObject.Find("Camera.main").GetComponent<MouseBoi>().enabled = false;
Cursor.lockState = CursorLockMode.None;
Cursor.visible = false;
}
you can write that complete code as
Cursor.visible = GameIsPaused;
GameObject.Find("Camera.main").GetComponent<MouseBoi>().enabled = false;
Cursor.lockState = CursorLockMode.None;
without the ifelse with the same result. At this point i assume you have a bug in your code as you probably want to set the MouseBoi Script to enabled=True when the Game is not paused anymore. You currently set it to False in both cases.
Further you should not use GameObject.Find
in Update
. This is really bad for performance, especially if you have a lot of objects in your scene. Cache the result in a variable on startup and then use that reference.
Oddly enough, I haven't set it in any other script, and even after I simplified that code, it still does the mouse-flickering thing.
good thing you managed to solve your problem. Please post what you did to solve this so that others might learn from this question.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Restricting movement with Mathf.Clamp 4 Answers
How do i give Coordinates (x, y) to an Array 1 Answer
How can i prevent from mouse to collide with thirdpersoncontroller ? 0 Answers