- Home /
Showing and Locking Cursor - JavaScript / JS
Hey there guys, I've never had any luck what so ever having a properly working Pause Menu, Inventory Menu, or anything else in relation to having a Visible Unlocked cursor....
The reason being is that the Cursor will never ever 'STAY' visible it either disappears very shortly after popping up and returns back to moving the camera, or it stays visible but as soon as i click anywhere on the screen it returns to the centre.
I am using Brakey's Inventory System, but as always with using other peoples scripts and what have you... It's outdated and is looking for the MouseLook Component... which as far as i know has not been in action on the FPC for the past 2 years.. and if so I've never seen it.
The exact thing I am trying to achieve is... Once 'Tab' is pressed, the game is completely Paused. (Sounds Paused, Time Scale 0, Camera cannot move when paused, the mouse is 'CONSTANTLY' available while paused, even after clicking on the game screen.)
I have provided a video to help provide a better resolution. https://youtu.be/SH_2ghWR_Eg (If the above video link does not work, it may still be Uploading)
If you watched the video you may see the errors within the code, but if not, here is the InvPauseGame.js from Brakeys Inventory System.
#pragma strict
#pragma downcast
var pauseGame = true; //Do we want to pause/unpause the game?
var disableMouseLookComponent = true; //Do we want to enable/disable the MouseLook component?
//These two variables are used when disabling/enabling the MouseLook component.
var ThePlayer : Transform;
var TheCamera : Transform;
var lockUnlockCursor = true; //Do we want to lock/unlock the mouse cursor?
private var lookAround01 : Behaviour;
private var lookAround02 : Behaviour;
@script AddComponentMenu ("Inventory/Other/Inv Pause Game")
function Awake ()
{
if (disableMouseLookComponent == true)
{
if (ThePlayer != null && TheCamera != null)
{
if (ThePlayer.GetComponent("MouseLook") != null || TheCamera.GetComponent("MouseLook") != null)
{
lookAround01 = ThePlayer.GetComponent("MouseLook");
lookAround02 = TheCamera.GetComponent("MouseLook");
}
else
{
disableMouseLookComponent = false;
}
}
else
{
disableMouseLookComponent = false;
}
}
}
function PauseGame (pauseIt : boolean)
{
//Locking the cursor
if (lockUnlockCursor == true)
{
if (pauseIt == true)
{
Screen.lockCursor = false;
}
else
{
Screen.lockCursor = true;
}
}
//Pausing the game
if (pauseGame == true)
{
if (pauseIt == true)
{
Time.timeScale = 0.0;
Time.fixedDeltaTime = 0.02 * Time.timeScale;
}
else
{
Time.timeScale = 1.0;
Time.fixedDeltaTime = 0.02 * Time.timeScale;
}
}
//Disabling the MouseLook component
if (disableMouseLookComponent == true)
{
if (ThePlayer != null && TheCamera != null)
{
if (pauseIt == true)
{
lookAround01.enabled = false;
lookAround02.enabled = false;
}
else
{
lookAround01.enabled = true;
lookAround02.enabled = true;
}
}
else
{
Debug.LogError("The variables of the 'InvPauseGame' script on '" + transform.name + "' has not been assigned.");
}
}
}
I haven't ever used Screen.lockCursor. I've always used the static variables in the Cursor class, and they've worked well. Give that a shot.
Hey thanks for the comment, i am still however having some issues with this.. no doubt I'm doing something wrong, but i went to the API about the Static Cursor variables and cannot get my head around the lockState Variable, yeh fair enough the Cursor.visable = true/false is easy as pie but.... whats all this? and how does this help me as to what im looking for... (im good at understanding things that are broken down, but when it comes to a whole bunch of random code that all looks the same in different cases just screws me over, and cannot make sense of it. eg. can this.... #pragma strict var wanted$$anonymous$$ode: CursorLock$$anonymous$$ode; // Apply requested cursor state function SetCursorState() { Cursor.lockState = wanted$$anonymous$$ode; // Hide cursor when locking Cursor.visible = (CursorLock$$anonymous$$ode.Locked != wanted$$anonymous$$ode); } function OnGUI() { GUILayout.BeginVertical(); // Release cursor on escape keypress if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Escape)) Cursor.lockState = wanted$$anonymous$$ode = CursorLock$$anonymous$$ode.None; switch (Cursor.lockState) { case CursorLock$$anonymous$$ode.None: GUILayout.Label("Cursor is normal"); if (GUILayout.Button("Lock cursor")) wanted$$anonymous$$ode = CursorLock$$anonymous$$ode.Locked; if (GUILayout.Button("Confine cursor")) wanted$$anonymous$$ode = CursorLock$$anonymous$$ode.Confined; break; case CursorLock$$anonymous$$ode.Confined: GUILayout.Label("Cursor is confined"); if (GUILayout.Button("Lock cursor")) wanted$$anonymous$$ode = CursorLock$$anonymous$$ode.Locked; if (GUILayout.Button("Release cursor")) wanted$$anonymous$$ode = CursorLock$$anonymous$$ode.None; break; case CursorLock$$anonymous$$ode.Locked: GUILayout.Label("Cursor is locked"); if (GUILayout.Button("Unlock cursor")) wanted$$anonymous$$ode = CursorLock$$anonymous$$ode.None; if (GUILayout.Button("Confine cursor")) wanted$$anonymous$$ode = CursorLock$$anonymous$$ode.Confined; break; } GUILayout.EndVertical(); SetCursorState(); }
be simply broken down to a single line command such as CursorLock$$anonymous$$ode.Confined/Locked/None;? because if so.. then how? I get an error of Assets/Inventory/Scripts/Other/InvPauseGame.js(93,44): BCE0034: Expressions in statements must only be executed for their side-effects.
(which does not explain itself to me)
PLEASE NOTE!!!!!!! i have refrained from using javascript throughout a lot of my projects, as i dislike it very much, it has no Visual Studio Referencing help (eg. every command line i write tells me there is an error when in fact that script works perfectly), and C# for me is so much easier to understand.
As stated in the question... I'm using someone else's scripts that would take me way.. way too long to recreate in C#. I am still a novice in JavaScripting, but would say I'm quite moderate in C#... so some simplification might be in order for me to understand this terrible messy way of coding(Sorry if i offend some people, but honestly javascript has been awful and full of flaws and bugs ever since i started coding RuneScape Private Servers back in 2008.
EDIT:::: Even with getting the code to work with no errors i still get the error of either the mouse disappearing if i click on the screen.... which i want it to remain where it was clicked and remain visible..... and if that doesnt happen when i press 'tab' to open the Inventory the cursor will pop up for a very very short time but disappear before i even get a chance to do anything.... this is the current code i have that is giving me the error of the mouse disappearing really quickly short after opening the Inventory screen...
if (ThePlayer != null && TheCamera != null)
{
Cursor.lockState = CursorLock$$anonymous$$ode.None;
Cursor.visible = true;
}
and btw if people are not answering this because there is so many questions already about this... well simply if people made more sense with helping people, and on top of that if unity stopped changing their API and just got something right for once. i went through every single question in relation on the first 3 pages on a few google searches... and not one of the 'Correct answers' helped me in any form.. there seems to be no working command/script for simply allowing the mouse to stay on screen for JS.... you would think it would be simple....
Try building the game and testing there. The editor has some overrides for mouse behaviour.
Thanks for the Comment, this however was done and tested before i posted the original question.. I like to eli$$anonymous$$ate everything i can possibly think of before i resort to asking a question.
first of all you have to test it in a build. the editor doesn't always respond correctly.
i have used this with no problems:
Screen.showCursor = false;
judging on how far along your game is. If i where you i would make my own cursor! this way you can do things like: snap it to places with code, move it with a joystick , prevent it from going off screen, keep in in certain areas, change the cursor image when things are clicked, and whatever else.
if you want a code example let me know
Hey there, thanks for the Comment, i did manage a build test before i posted the original question, but still that had no avail, it was the same as what the editor was doing.. That sounds awesome, an example code would be greatly appreciated!
// todd's custom cursor script
// very basic but should give you the idea
// drag and drop images into the inspector
public var cursorimage: Texture2D;
public var buttonimage: Texture2D;
var mx: float;
var my: float;
var r: Rect;
var speed: float;
var cursize: int;
var i: int;
var clicked: int;
//example buttons
var buttons: Rect[];
function Start () {
// seting coordinates of example buttons !!!
buttons=new Rect[3];
buttons [0] = new Rect (20, 10, 150, 50);
buttons [1] = new Rect (100, 150, 150, 50);
buttons [2] = new Rect (200, 300, 150, 50);
clicked = 100;
// size of cursor
cursize = 20;
//this makes a couple blank images if you didnt assign them yet
//you can delete these next two lines
if (cursorimage == null) {cursorimage = new Texture2D (1, 1);}
if (buttonimage == null) {buttonimage = new Texture2D (1, 1);}
}
function Update () {
// set the speed of the cursor
speed = 500 * Time.deltaTime;
// move with mouse
mx += Input.GetAxis ("$$anonymous$$ouse X")*speed;
my -= Input.GetAxis ("$$anonymous$$ouse Y")*speed;
//move with arrows keys and spacebar too
if(Input.Get$$anonymous$$ey("up")){my-=0.6*speed;}
if(Input.Get$$anonymous$$ey("down")){my+=0.6*speed;}
if(Input.Get$$anonymous$$ey("left")){mx-=0.6*speed;}
if(Input.Get$$anonymous$$ey("right")){mx+=0.6*speed;}
// dont let it goe off screen
if(mx>Screen.width-cursize){mx=Screen.width-cursize;}
if(my>Screen.height-cursize){my=Screen.height-cursize;}
if(my<0){my=0;}
if(mx<0){mx=0;}
if(Input.Get$$anonymous$$ouseButtonDown(0)){checkclick();}
if(Input.Get$$anonymous$$eyDown("space")){checkclick();}
}
function OnGUI(){
r = new Rect (mx, my,cursize, cursize);
GUI.DrawTexture (r, cursorimage);
i = buttons.Length;
while (i>0) {i--;
GUI.DrawTexture (buttons[i], buttonimage);
if(clicked==i){GUI.color=Color.red;}
GUI.Label(buttons[i],"button "+i);
GUI.color=Color.white;
}
}
// checks if we are over a button
function checkclick(){
var i2: int;
i2 = buttons.Length;
clicked=100;
while (i2>0) {i2--;
if(mx>buttons[i2].x&&mx<buttons[i2].x+buttons[i2].width){
if(my>buttons[i2].y&&my<buttons[i2].y+buttons[i2].height){
clicked=i2;
print("i pushed button number: "+clicked);
}}}
}
Answer by FuryFight3r · May 06, 2017 at 02:06 PM
I have found a simple solution to the error I am receiving, instead of attempting to enable/disable the mouselook component and seeing as though i will not be needing any of the FirstPersonControllers inner components such as walk speed, run speed, and jump speed because the game is 'paused', I have simply just re coded the script to disable the entire FirstPersonController Script while the game is paused, this works exceptional in both the Editor and in Build.
Your answer
Follow this Question
Related Questions
choppy mouse in webGL build 0 Answers
fps MouseLook with animation applied to camera. 1 Answer
disabling a script (MouseLook) 5 Answers
Mouse look script help 1 Answer