Post tutorial RSS How to hide the cursor?

How to hide the default system cursor in-game when it's not needed (in some platformers, etc.).

Posted by on - Basic Other

It's rather simple.

1. Create a script that and name it e.g. HideCursorScript, then edit it and paste the following code:

function Update () {
//hide the cursor (at the beginning of the game)
Screen.showCursor = false;

2. Attach your script to a game object, whether your character or simply the main camera - by dragging the script onto the object.

3. Then, to display the cursor, e.g. when pressing ESCAPE key to see the pause menu, you may use the code below:

var script = GetComponent("HideCursorScript"); 
script.enabled = false;

The full code that can be found in CarEdu source:

function Update () {

if(Input.GetKey("escape")) {
		//pause the game
		Time.timeScale = 0;
		//show the pause menu
		var script3 = GetComponent("PauseMenuScript"); 
		script3.enabled = true;
		//disable the cursor hiding script
		var script4 = GetComponent("HideCursorScript"); 
		script4.enabled = false; 

Location: Scripts/JavaScripts/UpdateScript.js
Also must be dragged onto the object.

4. In the resume button script there must be some code to activate the hiding script cursor back, see below:

var script4 = GetComponent("HideCursorScript"); 
script4.enabled = true;

The full code can be found here: Scripts/JavaScripts/PauseMenuScript.js
The script must be dragged on the object, too.

In CarEdu, all three scripts were dragged onto PauseMenuCamera object in CompleteScene scene.

Also note that HideCursorScript and UpdateScript are both enabled while PauseMenuScript is disabled by default. It's also very important, as enabling the pause menu script would show the pause menu immediately upon launching the game.

Summing everything up, notice these lines:
Screen.showCursor = false;
This line makes the cursor hidden.
Screen.showCursor = true;
And this one makes it shown.

These, along with enabling/disabling the short HideCursorScript script at the beginning are the core of hiding the cursor.


This comment is currently awaiting admin approval, join now to view.

Post a comment
Sign in or join with:

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.