Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by FuryFight3r · May 04, 2017 at 05:55 PM · javascriptmousemouselook

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.");
         }
     }
 }


Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FortisVenaliter · May 04, 2017 at 07:52 PM 0
Share

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.

avatar image FuryFight3r FortisVenaliter · May 05, 2017 at 04:08 AM 0
Share

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.

avatar image FuryFight3r FortisVenaliter · May 05, 2017 at 04:20 AM 0
Share

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....

avatar image FortisVenaliter FortisVenaliter · May 05, 2017 at 02:16 PM 0
Share

Try building the game and testing there. The editor has some overrides for mouse behaviour.

avatar image FuryFight3r FortisVenaliter · May 05, 2017 at 03:56 PM 0
Share

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.

avatar image toddisarockstar · May 05, 2017 at 03:21 PM 0
Share

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

avatar image FuryFight3r toddisarockstar · May 05, 2017 at 03:52 PM 0
Share

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!

avatar image toddisarockstar toddisarockstar · May 07, 2017 at 01:10 AM 0
Share
     // 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);
                 }}}
 
     }

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

106 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

have object rotate to the mouses screen position 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges