Quantcast
Channel: Questions in topic: "lockcursor"
Viewing all articles
Browse latest Browse all 92

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

$
0
0
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; } }

Viewing all articles
Browse latest Browse all 92

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>