Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Crucible1 · Jan 18, 2016 at 05:10 PM · screenmenulockpopup

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 ^_^

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vintar · Jan 18, 2016 at 07:27 PM 1
Share

make sure that the call for GetComponent().enabled = false; is on the same object as the mouselook script. It should work.

avatar image Crucible1 vintar · Jan 19, 2016 at 01:54 PM 0
Share

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!

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

36 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges