- Home /
Locking cursor to middle of screen not working
Hey!
So I'm attempting to make a script that locks the mouse cursor to the center of the screen. I'm trying to use a method that sets a bool to true if "F" is pressed. If said bool is set true, the cursor is then locked to the middle of the screen. Sadly, it's just not working. Here is my code:
using UnityEngine;
using System.Collections;
public class CursorLock : MonoBehaviour {
public bool cursorLocked;
void Start () {
cursorLocked = false;
Cursor.visible = true;
}
void Update () {
if (cursorLocked) {
Cursor.lockState = CursorLockMode.Locked;
} else {
Cursor.lockState = CursorLockMode.None;
}
}
bool CursorCheck (bool cursorLocked) {
if (Input.GetKey (KeyCode.F)) {
cursorLocked = true;
} else {
cursorLocked = false;
}
return cursorLocked;
}
}
Originally, I had my script set to call the method in the Update like this:
using UnityEngine;
using System.Collections;
public class CursorLock : MonoBehaviour {
public bool cursorLocked;
void Start () {
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
void Update () {
bool cursorState;
CursorCheck cursorCheck = new CursorCheck ();
cursorState = cursorCheck.CursorCheck (cursorLocked);
if (cursorState) {
Cursor.lockState = CursorLockMode.Locked;
} else {
Cursor.lockState = CursorLockMode.None;
}
}
bool CursorCheck (bool cursorLocked) {
if (Input.GetKey (KeyCode.F)) {
cursorLocked = true;
} else {
cursorLocked = false;
}
return cursorLocked;
}
}
But all that did was give me this error:
"Assets/Scripts/CursorLock.cs(17,17): error CS0118: 'CursorLock.CursorCheck(bool)' is a 'method' but a 'type' was expected"
Maybe I'm using return methods wrong? Any help is appreciated!
Answer by JigneshKoradiya · Apr 11, 2016 at 06:19 AM
using UnityEngine;
using System.Collections;
public class CursorLock : MonoBehaviour {
public bool cursorLocked;
void Start () {
cursorLocked = false;
Cursor.visible = true;
}
void Update ()
{
if (Input.GetKey (KeyCode.F))
{
cursorLocked = true;
}
else
{
cursorLocked = false;
}
if (cursorLocked) {
Cursor.lockState = CursorLockMode.Locked;
} else {
Cursor.lockState = CursorLockMode.None;
}
}
}
Your answer
Follow this Question
Related Questions
Box goes through walls 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Script on prefab keeps values from one execution to another 1 Answer
how to change gameobject color in hierarchy for highlight purposes. 3 Answers