• 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
2
Question by fajefaje2 · Mar 02, 2015 at 11:00 AM · c#unity 5javascriptscreenlockcursor

Lock Cursor problem in Unity 3D 5.

Lock Cursor problem in Unity 3D 5.

Hi there I have problem with Lock Cursor in Unity 3D 5 in C#

Replace " Screen.lockCursor = true; " on " Cursor.lockState = true; " and shows an error " error CS0029: Cannot implicitly convert type bool' to UnityEngine.CursorLockMode' ". Tell me how to fix the error and how to move the cursor, and that he was in the middle of the screen. Need for the shooter.

Respond fast please !!!

Cheers !!!

Comment
Add comment
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

5 Replies

· Add your reply
  • Sort: 
avatar image
11

Answer by maccabbe · Mar 02, 2015 at 11:09 AM

The error is telling you that Cursor.lockState is not a bool but an enum of type CursorLockMode. Looking in the unity documentation you can see that CursorLockMode can have 3 values, None, Locked, and Confined. Therefore use the following line

 Cursor.lockState = CursorLockMode.Locked;

http://docs.unity3d.com/500/Documentation/ScriptReference/CursorLockMode.html

Comment
Add comment · Show 2 · 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
avatar image UltimateTactix · May 15, 2015 at 09:02 AM 0
Share

lockState doesn't exist for me, am I doing something wrong? (Probably :p)

avatar image maccabbe · May 15, 2015 at 06:20 PM 0
Share

What version of Unity are you using?

avatar image
2

Answer by elcast_cze · Jun 19, 2015 at 03:56 AM

https://www.youtube.com/watch?v=2bWoRTRT_DU

 using UnityEngine;
 using System.Collections;
 
 public class HideLockCursorA : MonoBehaviour
 {
 void Start ()
   {
     Cursor.lockState = CursorLockMode.Locked;
     Cursor.visible = false;
   }
                   void Update ()
             {
                     Cursor.lockState = CursorLockMode.Locked;
                     Cursor.visible = false;
             }
 }
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
avatar image
0

Answer by TexasFunk101 · May 17, 2015 at 06:31 PM

Here is what I did for my lock cursor script

 function Start ()
     {
     Screen.lockCursor = true;
     UnityEngine.Cursor.visable = false;
     }

Comment
Add comment · Show 1 · 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
avatar image Digestivbiscuitsm · Jul 29, 2017 at 10:39 AM 0
Share

Screen.lockCursor can't be used anymore since it is obsolete.

avatar image
0

Answer by Ziron999 · Sep 04, 2015 at 10:25 AM

You have to have it in update at t$$anonymous$$s time due to a bug in unity where it loses focus. Hopefully they will fix t$$anonymous$$s someday....Unfortunately currently the only way w$$anonymous$$ch is bad for performance is t$$anonymous$$s way from what I can tell. You could detect and create an void OnFocus but that would be OS dependent :( In other words it would be different if it's a Mac/Phone/Windows/ect ect. So t$$anonymous$$s is the only practical solution for when you lose focus(basically alt+tab) constantly.

 void Update()
     {
         if (Cursor.lockState != CursorLockMode.Locked)
         {
             Cursor.lockState = CursorLockMode.Locked;
             Cursor.visible = false;
         }
     }
Comment
Add comment · Show 1 · 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
avatar image Ziron999 · Sep 04, 2015 at 10:28 AM 0
Share

I should add that it's still weird there is a bug with cursor.visable it will lock into the center but will not go invisible it may be best to not even use cursor.visable until unity dev's get this stuff straight...

avatar image
0

Answer by Digestivbiscuitsm · Jul 29, 2017 at 10:42 AM

Well, the script that I used was t$$anonymous$$s:

 void Update()
 {
         //Mouse lock and confine
         Cursor.visible = false;
         Cursor.lockState = CursorLockMode.Locked;
         Cursor.lockState = CursorLockMode.Confined;
 
 //and to unlock it is t$$anonymous$$s:
         Cursor.lockState = CursorLockMode.None;
         Cursor.visible = true;
 }

I hope t$$anonymous$$s helps!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

How to make an interactive App to play on 4 different monitor? 0 Answers

How To make a Ui button Chance colours when pressed 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Unity 3D - Rotating an object in relation to its velocity 0 Answers


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