- Home /
Question by
Lbalint2007 · Mar 24, 2019 at 03:58 PM ·
cursorgetbutton
Get button down disable and enable cursor
Hi! Im trying to make a code. Its a long script, but here's the part that u need to know:
if(Input.GetButton("F1")){ Cursor.lockstate = CursorLockMode.None; Cursor.visible = true; }
What i'm trying to make, is if i press it for the first time, it enables it, and when i press it for the second time, it disables it.
I need your help!
Comment
Answer by WarmedxMints · Mar 24, 2019 at 04:03 PM
Just declare a bool and toggle its state when F1 is pressed
private bool _lockCursor;
public void Update()
{
if(Input.GetKeyDown(KeyCode.F1))
{
//Toggle the bool state
_lockCursor = !_lockCursor;
//Set cursor lockstate
Cursor.lockState = _lockCursor ? CursorLockMode.Locked : CursorLockMode.None;
//Set cursor visible state
Cursor.visible = !_lockCursor;
}
}
Answer by Lbalint2007 · Mar 24, 2019 at 04:04 PM
Thank you!
@Lbalint2007 This section is for answers. You should post replies in the comment sections. Can you also please mark the question as answered if your problem is solved, please so others will know you have a solution to your question.