Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Irsan · Jan 13, 2011 at 06:50 AM · mousecursorlock

Locking the Cursor at the center.

I've been looking at answers, and I don't understand them. All I want is a script that will lock the cursor at the center of the screen for a stand-alone game. Just one script, NOTHING ELSE.

Comment
Add comment · Show 2
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 yoyo · Jan 13, 2011 at 09:26 AM 1
Share

Except for the simplest of things, it rarely works to simply drop a script into a project and have it do what you want, and your request is non-trivial. You're probably best off studying people's proposed answers to see what might work best in your situation, and perhaps finding a programmer friend you can talk to face to face to explain some of the more intricate details.

avatar image Rocketman123 · Oct 19, 2016 at 08:03 PM 0
Share

i would say (from my expert knowledge. J$$anonymous$$.) that you would use this (C#)

     Screen.lockCursor = false;

to unlock the cursor from the center. to lock it :

     Screen.lockCursor = true;

`

and to show the cursor on the screen :

     Cursor.visible = true;

and to make it unvisible :

     Cursor.visible = false;

hope this helps!

4 Replies

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

Answer by juan_txo · Jan 13, 2011 at 10:53 AM

Hello.

I needed what you want during a testing proyect. I used "Screen.lockCursor" to block the cursor in the center of screen to simulate the pointer of a gun.

Here you have the API example: ScreenLookCursor

I hope its what you want. See you.

Comment
Add comment · Show 4 · 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 adriansnetlis · Apr 26, 2015 at 06:53 AM 1
Share

Hi! Does it woek also in Unity 5.0.1?

avatar image AlucardJay · Apr 26, 2015 at 11:41 AM 0
Share

Check the documentation, this will tell you : http://docs.unity3d.com/ScriptReference/Screen-lockCursor.html

Obsolete : Property lockCursor has been deprecated. Use Cursor.lockState and Cursor.visible ins$$anonymous$$d.

Always check the Unity Scripting Reference !

Cursor.lockState : http://docs.unity3d.com/ScriptReference/Cursor-lockState.html

Cursor : http://docs.unity3d.com/ScriptReference/Cursor.html

avatar image AlucardJay · May 26, 2015 at 06:31 AM 0
Share

@andrewjw : Screen.lockCursor and Cursor.lockState are persistent, once it is set you should never have to set them for every Update nor OnGUI.

The only time you would see the cursor flickering is when running in the editor. Screen.lockCursor never worked properly in the editor. This is why you should build your project to see what result the end user will see.

link to example of a tutorial using a toggle cursor command in Update?

avatar image andrewjw · May 26, 2015 at 01:03 PM 0
Share

Sorry, I'm wrong... they just don't work right in the editor.

avatar image
3

Answer by Nsmurf · Jul 18, 2012 at 06:33 PM

 void Update()
 {
 if (Input.GetKey(KeyCode.Escape))
     Screen.lockCursor = false;
 else
     Screen.lockCursor = true;
 }

Press and hold escape to unlock the mouse.

Comment
Add comment · Show 3 · 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 Melvin8D · Oct 22, 2014 at 12:37 AM 0
Share

is this JS or c#?

avatar image kungfooman · Mar 11, 2015 at 07:45 AM 1
Share

This doesn't work with Unity 5 anymore. -.-

avatar image SectraGaming kungfooman · Oct 22, 2017 at 02:47 AM 0
Share

It does just add it to your mouse look in the update section I posted the code on this fourm!

avatar image
0

Answer by Ravenmore · May 12, 2012 at 10:37 AM

I've been looking at threads about this problem and haven't found an easy enough answer, so I thought I'd add my two cents:

It's not a perfect solution bot for prototyping purposes I use this:

 function Update () {
     Screen.lockCursor = true;
     Screen.lockCursor = false;
 }

Screen.lockCursor moves the pointer to the center of the screen, so it does almost exactly what the OP suggested - resets the cursor every frame.

You could also use a MouseMove event for example :)

Not very clean but it does the job, you retain OnMouseDown functionality, the cursor remains visible.

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 Toadill9114 · Dec 07, 2016 at 11:02 PM 1
Share

Screen.lockCursor is depreciated you must now use Cursor.lockState and Cursor.visible ins$$anonymous$$d.

This means you will have to change code like Screen.lockCursor = true; and Screen.lockCursor = false; to Cursor.lockState = CursorLock$$anonymous$$ode.Locked; and Cursor.lockState = CursorLock$$anonymous$$ode.None;

avatar image
0

Answer by SectraGaming · Oct 22, 2017 at 02:44 AM

Every thing below this line!

 public float mouseSensitivity = 100.0f;
 public float clampAngle = 80.0f;

 private float rotY = 0.0f; // rotation around the up/y axis
 private float rotX = 0.0f; // rotation around the right/x axis

 void Start ()
 {
     Vector3 rot = transform.localRotation.eulerAngles;
     rotY = rot.y;
     rotX = rot.x;
 }

 void Update ()
 {

     if (Input.GetKey(KeyCode.Escape))
         Screen.lockCursor = false;
     else
         Screen.lockCursor = true;
     
     float mouseX = Input.GetAxis("Mouse X");
     float mouseY = -Input.GetAxis("Mouse Y");

     rotY += mouseX * mouseSensitivity * Time.deltaTime;
     rotX += mouseY * mouseSensitivity * Time.deltaTime;

     rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);

     Quaternion localRotation = Quaternion.Euler(rotX, rotY, 0.0f);
     transform.rotation = localRotation;
 }

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

7 People are following this question.

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

Related Questions

Screen.lockCursor messes my rotation 1 Answer

Locking cursor/mouse on an object 1 Answer

Hide and lock the mouse cursor (beginner) 0 Answers

How to lock mouse? 1 Answer

Locking the mouse cursor without it centering after unlocking it? 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