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 Mad Grendel · Aug 30, 2014 at 03:12 AM · locklock-cursorlockcursor

How do I lock and then unlock the cursor using the same key?

All I want to do is lock the cursor to the middle of the screen at game start. That part is easy and it works. But no matter what I do, I cannot get a script to properly lock and unlock the cursor again, from the center of the screen, using just one key like 'R' for example. I have looked everywhere and read about every post I can find and this seems to be such a simple thing to do but it doesn't work.. Does anyone know how to script this properly? Here is my code and I have no idea why it wouldn't work but I am obviously misunderstanding something about the update function?

Also where should I be attaching this script - to the Player, the Player's main camera? Something else? This is an FPS styled game similar to Elder Scrolls and most survival horror games like Amnesia etc. I'd also like to get the tab key to bring up a menu eventually with the cursor unlocked of course but first things first... Thanks for any help, it's driving me crazy that I can't get this seemingly simple thing to work.

 #pragma strict
 
 private var wasLocked : boolean;

 // lock the cursor at game start and set wasLocked to true. I'm thinking
 // that we need to have two different states to check for when locking and 
 // unlocking within the Update method - we're using the same key to lock and unlock 
 // so I'm trying to use a boolean variable to add to the check.

 
 function Start(){
     Screen.lockCursor = true;
     Screen.showCursor = false;
     wasLocked = true;
 }
 
 function Update () {
     
     // Unlock the cursor
     if (Input.GetKeyDown (KeyCode.R) && wasLocked == true){
         Screen.lockCursor = false;
         Screen.showCursor = true;
         wasLocked = false;        
     }

     // Lock the cursor back to center of screen?  
     if (Input.GetKeyDown (KeyCode.R) && wasLocked == false){
         Screen.lockCursor = true;
         Screen.showCursor = false;
         wasLocked = true;        
     }    
 }




Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by supernat · Aug 30, 2014 at 03:21 AM

It's probably a misunderstanding of the compiler of what you really mean. It probably thinks you mean this:

(( Input.GetKeyDown (KeyCode.R) && wasLocked) == false)

It's good practice to surround your logic statements with explicit precedence, like this:

(Input.GetKeyDown (KeyCode.R) && (wasLocked == false))

Now, doing a test of something == false is not necessary. Use this:

(Input.GetKeyDown (KeyCode.R) && (!wasLocked))

Note that I surrounded the !wasLocked with (), but had I not done that, the compiler is smart enough to know you want to test !wasLocked first, so placing () around !variable is a matter of debate. I do it just to be explicit, but it's highly unlikely the compiler will be modified in a way that would break this. But with your original code, the compiler tested key down and wasLocked and looked for that to be false. You also need to correct the true line as well.

The simpler case is to just check if (Input.GetKeyDown(KeyCode.R)) first, then in a sub code block test for wasLocked or not.

Edit: you can place this on any GameObject, as long as that game object will be alive for the entire time that you want this behavior to be active. Just make sure it's not on 2 or more game objects, then it will just toggle on and off, for even numbers of game objects anyway.

Comment
Add comment · 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
0

Answer by Addyarb · Aug 30, 2014 at 03:22 AM

Try this:

 if(Input.GetKeyDown(KeyCode.R)){
     screenLock = !screenLock;
     }
     
     if(screenLock){
     Screen.lockCursor = true;
     Screen.showCursor = false;
     }
     else{
     Screen.lockCursor = false;
     Screen.showCursor = true;
     }
 
Comment
Add comment · 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

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

Screen.LockCursor not working in Windows Store apps? 0 Answers

How can i lock the mouse cursor in the middle of the screen ? 1 Answer

How to make the basic mouselook look all the way up and down? 2 Answers

Cant unlock cursor with FPS prefab 2 Answers

Can you lock the cursor to specific coordinates? 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