3D Survival FPS Game
Making the Mouse Cursor Invisible during game play
Now that we have the basic character movement and also camera movement pat down, we need to start adding some features to this barebones FPS prototype. The first one is quite simple and that is to remove the mouse cursor from the screen when the game plays, and when the “Escape” button is pressed, the cursor reappears.
Scripting API:
Within Unity’s scripting API there is a function called “Cursor.Visiable” which makes the cursor arrow disappear when the game is running. Unfortunately because the mouse function when clicking stuff is still active it ruins the gameplay.
Luckily Unity has another function called “Cursor Lock Mode” which also hides the mouse arrow along with disabling any on click abilities . It also locks the arrow within the center of the screen.
Scripting the Lock Cursor:
The scripting is actually quite simple, within the player script, during the start method, we are simple going to set the lock mode for the cursor to be “locked”, this centers the arrow and also makes it invisible.
The to toggle the arrow back up again, we are going to create a new private method called Unlock Cursor. Within this method we are going to use an IF statement checking whenever there is a escape button press. If there was then we will set the cursor lock state to be None.
I also added a global bool to set whenever the cursor has been made visible. Then I created another IF statement to make the cursor return back to the lock state when I pressed the escape key again.