How to lock screen during menu pop up?
Hi,
I'm having trouble locking my screen when my UI menus pop up. At the moment when they appear, as I am using first person mouselook, my character looks all over the place when trying to select a button.
I'm on Unity 5 so Screen.lockCursor = true doesn't work for me :(
Im guessing that the code will go in the script managing the UI canvas, (if not please say and I'll post the script that is needed), so here is what I've got:
Canvas canvas;
void Start()
{
canvas = GetComponent<Canvas>();
canvas.enabled = false;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
Pause();
}
}
public void Pause()
{
canvas.enabled = !canvas.enabled;
Time.timeScale = Time.timeScale == 0 ? 1 : 0;
GetComponent<MouseLook>().enabled = false;
}
So basically what I want to have happen is that the screen becomes locked when these pop ups appear so that the users can easily select a button without mouselook sending them all over the screen.
Any help will be greatly appreciated! Thank you ^_^
make sure that the call for GetComponent().enabled = false; is on the same object as the mouselook script. It should work.
Thanks!
I changed some of the code (just to streamline it so that it was only about locking the screen) and I attached it to the $$anonymous$$ouseLook and it worked! (yay).
The only thing now is that, after the pop up has gone away, the $$anonymous$$ouseLook is still frozen. I have an idea of what to write (something like an 'else' option right?) but nothing I try seems to work, any ideas what to do?
The code now looks like this (I've added in my other popup option so that they are all together) :
using UnityEngine; using System.Collections;
public class LockScreen : $$anonymous$$onoBehaviour {
void Update()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Escape))
{
GetComponent<$$anonymous$$ouseLook>().enabled = false;
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$))
{
GetComponent<$$anonymous$$ouseLook>().enabled = false;
}
}
}
Also, is there a way to have this work with Raycast? Y'see I also bring up a UI with $$anonymous$$ouse 0 so is there a way to specify a locked $$anonymous$$ouselook when clicking a button, rather than whenever $$anonymous$$ouse 0 is clicked?
Here is the coding for that too:
public class OnClick : $$anonymous$$onoBehaviour
{
Canvas canvas;
void Start()
{
canvas = GetComponent<Canvas>();
canvas.enabled = false;
}
void Update()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.$$anonymous$$ouse0))
{
// Create a ray going from camera to the screen point
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
// Check if the ray hits a collider
if (Physics.Raycast(ray, out hit))
{
// Check if the collider we hit is attached to the current GameObject
if (hit.collider.gameObject == gameObject)
{
Play();
}
}
}
}
public void Play()
{
canvas.enabled = !canvas.enabled;
Time.timeScale = Time.timeScale == 0 ? 1 : 0;
GetComponent<$$anonymous$$ouseLook>().enabled = false;
}
}
Thank you for your help!
Your answer
Follow this Question
Related Questions
[Win][Editor] How can I lock inputs to the Game in a menu ? (canvas) 0 Answers
I have a problem with the pop-up lose menu and win menu Please help!! 0 Answers
How Do I Make the Camera Follow an Escape ship. 0 Answers
Why my Scale Script don't work ? 1 Answer
Scene rotating in game mode when is kinematic is unchecked 0 Answers