I don’t know anything about coding or scripting or anythign like that beyond basic HTML (not even CSS). I was posting in someone else’s thread but I didn’t want to steal their thread and I wasn’t getting much help there, so I thought I’d start my own.
Basically I need to hide and lock the mouse cursor so that the player doesn’t see their arrow floying all over while they’re trying to control the FPS style game.
What I did:
- Right-click the Scripts folder in the project tab and choose Create > JavaScript
- Click that Script and choose Edit in the Inspector
- Paste in this script and hit Save.
- Create an empty game object.
- Drag the script from the Project tab onto the game obejct
- Run the game
Am I missing something? It doesn’t seem to do anything at all. I have also tried building a stand along Windows player and a web player. Neither of them have the cursor hidden.
That script locks the cursor in the game window, but it doesn’t hide it. I added some Screen.showCursor settings to that script. Does it make sense where I put them and why?
// Called when the cursor is actually being locked
function DidLockCursor () {
Debug.Log("Locking cursor and hiding it");
// Disable the button
guiTexture.enabled = false;
}
// Called when the cursor is being unlocked
// or by a script calling Screen.lockCursor = false;
function DidUnlockCursor () {
Debug.Log("Unlocking cursor");
// Show the button again
guiTexture.enabled = true;
}
function OnMouseDown () {
// Lock the cursor
Screen.lockCursor = true;
// And hide it
Screen.showCursor = false;
}
private var wasLocked = false;
function Update () {
// In standalone player we have to provide our own key
// input for unlocking the cursor
if (Input.GetKeyDown ("escape"))
Screen.lockCursor = false;
Screen.showCursor = true;
// Did we lose cursor locking?
// eg. because the user pressed escape
// or because he switched to another application
// or because some script set Screen.lockCursor = false;
if (!Screen.lockCursor wasLocked) {
wasLocked = false;
DidUnlockCursor();
}
// Did we gain cursor locking?
else if (Screen.lockCursor !wasLocked) {
wasLocked = true;
DidLockCursor ();
}
}
Thank you. It makes sense. I wonder now though if an FPS type game really needs the cursor to be locked. Will that stop the character from being able to turn all the way around?
All I need is that the cursor be hidden for the stand alone player and the web player.
OK I tried that script and it still doesn’t do anything. It plays exactly the same as it does with no script at all.
In another thread the user Discord posted this code, which works and hides the cursor nicely, but only for the stand-alone, not the web player:
function Start()
{
Screen.lockCursor = true;
}
function Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
Screen.lockCursor = false;
}
}
The doc page for Screen.lockCursor mentions that the lock is only obtained after the user has clicked on the player and while the cursor is still within the player area. Could this explain what is going wrong in your case?
I don’t think so, I’ve tried clicking all over it. The cursor remains visible.
Hey Phil. I recognize you from the LW/3dc forums I think.
Have you tried putting:
Screen.showCursor = false;
in your script?
Yes, that would be me. I have not tried that, I will but I’m not sure where it would go in the script I posted right above.
Sorry, bad explanation.
I placed it in the Start() function of the MouseLook script that’s attached to MainCamera object in the Island demo, did a Build and Run to Windows and got pretty good results.
Well the thing is, the code I posted above works great for hiding the mouse in the stand-alone Windows version, but it doesn’t work for the web player. Have you tried it with that?
I have and it performs exactly how I would expect it to perform. The mouse is not seen when it is within the play area and appears upon moving it outside but you retain control.
That doesn’t sound right. With an FPS you should be able to continuously move the mouse in any direction without any disruption in game play. The mouse should stay hidden until the player needs it.
That is nothing to do with Unity though. You simply cannot stop that effect in a web browser without some other external effect on the mouse. Try some javascript in the actual web page behind the game to hide the mouse.
But even that will only work while the mouse is in the browser main window. As soon as you move enough to leave the page and move the mouse over the desktop it will re-appear. So you may need to use an external library inside the Unity editor or load a tiny java file in the background that disables the mouse.
Try hiding the mouse and detecting if the mouse is at the edge of the screen, if it is, move it back to the inside edge.
The only problem with all this is that players would need to ALT-F4, ALT-TAB or CTRL-ALT-DEL > Close Process to get the mouse back. lol.
You’re right, I’m sorry I thought I had seen some of the Showcase web games with no cursor but I looked again and they all seem to have it.