Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by AndroidWG · Jul 27, 2014 at 09:19 PM · menupausepause menulock-cursor

Locked Cursor when Game Paused

I'm brazillian, don't look for error in my text ;)

Hey Guys! I'm doing an 3D Isometric plataform game, and I'm having a problem with the pause menu. The Pause Menu works fine, but i want to when you enter in the menu, the cursor get unlocked and when you get out the menu, the cursor get locked again. This is the original pause menu code:

 #pragma strict
 
 var pause : boolean = false;
 var pauseGUI : GUIText;
 pauseGUI.enabled = false;
 
 function Update(){
     if(Input.GetKeyUp(KeyCode.Escape)) {
         if(pause==true){
             pause = false;
         }
         else {
             pause = true;
         }
         if(pause == true) {
             Time.timeScale = 0.0;
             pauseGUI.enabled = true;
         }
         else {
             Time.timeScale = 1.0;
             pauseGUI.enabled = false;
         }
     } 
 }

So i changed to this:

 #pragma strict
 
 var pause : boolean = false;
 var pauseGUI : GUIText;
 pauseGUI.enabled = false;
 
 function Update(){
     if(Input.GetKeyUp(KeyCode.Escape)) {
         if(pause==true){
             pause = false;
         }
         else {
             pause = true;
         }
         if(pause == true) {
             Screen.lockCursor = false;
             Time.timeScale = 0.0;
             pauseGUI.enabled = true;
         }
         else {
             Time.timeScale = 1.0;
             pauseGUI.enabled = false;
             Screen.lockCursor = true;
         }
     } 
 }

When I press ESCAPE, the menu open and the cursor is unlocked. But when i press ESCAPE again, the menu close and the cursor still unlocked! This is the ULTRA basic code I made to lock the cursor when open scene:

 #pragma strict
 
 function Start () {
     Time.timeScale = 1.0;
     Screen.lockCursor = true;
 }
 
 function Update () {
 
 }

Can someone help me? And explain why this happen? Thanks a lot!

Comment
Add comment · Show 1
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 Kiwasi · Jul 31, 2014 at 12:27 AM 0
Share

Are you working inside the editor?

Often the editor will unlock the cursor to let you work on the editor menus while the game is running. Try clicking inside the game window and see if your cursor returns to locked

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sethuraj · Jul 29, 2014 at 12:35 PM

Its a simple mistake you made.Take a closer look in the second code snippet.In both conditions you set 'Screen.lockCursor=true'

 if(pause == true) 
 {
     Screen.lockCursor = true;   //You set this to true
     Time.timeScale = 0.0;
     pauseGUI.enabled = true;
         
 }       
 else 
 {
     Time.timeScale = 1.0;
     pauseGUI.enabled = false;
     Screen.lockCursor = true; //Here also you set this to true
 }


Change it to this

 #pragma strict
  
 var IsPaused : boolean = false;
 var pauseGUI : GUIText;
 pauseGUI.enabled = false;
  
 function Update()
 {
     if(Input.GetKeyUp(KeyCode.Escape)) 
     {
         //If game is already paused,resume it
         if(IsPaused)
         {
             //Set timescale to 1.0
             Time.timeScale = 1.0;
             //Disable GUI
             pauseGUI.enabled = false;
             //Lock the cursor
             Screen.lockCursor = true;
 
             //State the game is resumed
             IsPaused = false;
         }
         //Else pause the game
         else 
         {
             //Set timesclae to 0.0            
             Time.timeScale = 0.0;
             //Enable GUI
             pauseGUI.enabled = true;
             //Unlock the cursor
             Screen.lockCursor = false;
 
             //State the game is paused
             IsPaused = true;
         }
     }
 }


Comment
Add comment · Show 3 · Share
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 AndroidWG · Jul 29, 2014 at 08:51 PM 0
Share

Sorry, but it didnt worked, for some reason.

avatar image sethuraj · Jul 30, 2014 at 04:49 AM 0
Share

What's the problem you are getting now..?.you also need to add this code in the beginning to hide the cursor

 function Start () 
 {
     Screen.lockCursor = true;
 }

avatar image AndroidWG · Jul 31, 2014 at 12:01 AM 0
Share

I know this. I put this in the function Start but it dosent work. Same problem that I said in the question.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Problem with Pause and Pause Menu Script 1 Answer

Pause Menu open/close on key 1 Answer

How to Appease a pause menu in a game that changes it's time scale? 1 Answer

GUI Buttons not working after scene transition 0 Answers

Game stopping at Pause? 1 Answer


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