How do I relock my cursor?
I found this code online once and I'm trying to put in some lines that can relock the cursor but it isn't working. Any help?
using UnityEngine;
using System.Collections;
public class CharacterMovement : MonoBehaviour {
// A boolean to check if the cursor is locked.
bool IsLocked;
void Start ()
{
// The mouse is invisible when the game is started.
Cursor.lockState = CursorLockMode.Locked;
// The cursor is locked.
IsLocked = true;
}
void Update ()
{
// To see the mouse again after pressing a certain key.
if (IsLocked == true){
if(Input.GetKeyDown("escape")){
// The cursor is freed!
Cursor.lockState = CursorLockMode.None;
// A boolean for relocking.
IsLocked = false;
}
}
///This (and the IsLocked boolean) is what I added.
// To relock the cursor.
if(IsLocked == false){
if(Input.GetKeyDown("escape")){
// The cursor is locked!
Cursor.lockState = CursorLockMode.Locked;
// The boolean again.
IsLocked = true;
}
}
Comment
Your answer
Follow this Question
Related Questions
Lock the cursor inside the scene editor window ? 0 Answers
Unity 2020+ Project: Cursor Lock Requires Additional Click 0 Answers
Code acts differently in new project than in old project; Cursor.lockState issue 1 Answer
How do I detect if my mouse is over UI? 0 Answers
Get vector3 from cursor position relative to object in 3D plane 1 Answer