Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
6
Question by MC HALO · May 04, 2011 at 02:30 AM · mousecursor

how to stop mouse going out of the screen ?

Hi, guys i was wondering if you could please help me. Basically the problem that i am having is with my mouse cursor. what i want to do is if the player runs the game in the build view and his window is in not in full mode i want to stop them from leaving the screen. so what i mean is when ever you make a build and run it you can always get you mouse cursor out of the screen and start touching the X button and Minimize and etc. So what i want to do is if the game starts the mouse is hidden and if they try to go out of the screen i don't want that to happen. Unless they pause the game and the mouse cursor will go back the way it was.

here what i have come up with so far dont laugh lol i will be really happy if some one could really help me :-) thanks in advance MCHALO :)

My script:

function Awake() {

 Screen.showCursor = false;


}

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 Dreamer · May 04, 2011 at 02:46 AM 0
Share

2 approaches I can think of: 1. Try to find if there is such settings in editor 2. If not, then a editor script is required to do it. (I don't noe how to write one)

avatar image MC HALO · May 04, 2011 at 02:56 AM 0
Share

:) thanks for the ideas :)

10 Replies

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

Answer by Joshua · May 04, 2011 at 03:40 AM

All you can do is use Screen.lockCursor to lock the cursor in the center of the screen and hide it. Don't just do it in your Awake function because if a player presses escape you'll have to reinitialise it. Do it OnMouseDown possibly.

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 MC HALO · May 04, 2011 at 04:07 AM 1
Share

Does not work :) the mouse is still moveable and goes out of the screen :)

avatar image Joshua · May 04, 2011 at 12:17 PM 1
Share

Of course it does work, how can you say it doesn't. It's a piece of code that does EXACTLY what you asked. You're just using it wrong. Like I said don't stick it in Start or Awake.

avatar image mchalo · Jun 08, 2011 at 06:13 AM 0
Share

I solved the problem my self i did it using start and update but thanks for the help.

avatar image Shinugami · Nov 18, 2013 at 02:29 PM 1
Share

Thanks $$anonymous$$CHalo. I had the same problem, I put:

Screen.lockCursor = true;

Despite being full-screen and stating that code in the start function the mouse would still go onto my other screen in-game. As you said, putting that code in the Update(){} function solved the problem. Thankyou.

avatar image
41

Answer by jcrosby · Apr 05, 2016 at 05:37 AM

I know this is an old answer, but it helps to keep old answers update with current information. I found a much easier and up to date solution:

 Cursor.lockState = CursorLockMode.Confined; // keep confined in the game window
 Cursor.lockState = CursorLockMode.Locked;   // keep confined to center of screen
 Cursor.lockState = CursorLockMode.None;     // set to default default
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 Monkiman300 · Jul 06, 2016 at 09:06 PM 1
Share

I had to log in to say THAN$$anonymous$$ YOU for this. This is absolutely the correct solution, not the chosen one. I was able to use Cursor.lockState = CursorLock$$anonymous$$ode.Confined; and it keeps my mouse within the boundaries of the screen no matter the resolution... Even in windowed mode!

  • $$anonymous$$onkiman300

avatar image Oni07R · May 14, 2018 at 03:11 PM 0
Share

Thank you! Simple and perfect answer! Too bad the chosen answer is not yours :/

avatar image Aspect13 · Jan 07, 2019 at 09:10 PM 0
Share

Quite late. Thanks though. Amazing and the most correct answer. Thank you so much.

avatar image
6

Answer by amirabiri · Jun 19, 2012 at 01:15 AM

I found a better solution for this problem using PInvoke. This would work only on windows, but windows is the main platform that suffers from this problem I believe:

 #if UNITY_STANDALONE_WIN
     [DllImport("user32.dll")]
     static extern bool ClipCursor(ref RECT lpRect);

     public struct RECT
     {
         public int Left;
         public int Top;
         public int Right;
         public int Bottom;
     }
 #endif

 public void Start()
 {
     RECT cursorLimits;
     cursorLimits.Left   = 0;
     cursorLimits.Top    = 0;
     cursorLimits.Right  = Screen.width  - 1;
     cursorLimits.Bottom = Screen.height - 1;
     ClipCursor(ref cursorLimits);
 }
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 amirabiri · Jun 19, 2012 at 01:46 AM 1
Share

Just to clarify, this solution would limit the mouse cursor to a certain region at the operating system level, giving a smooth result without compromising the OS' natural mouse movement.

avatar image Spoink · Dec 16, 2013 at 12:08 PM 0
Share

Great stuff! Very neat solution to our problem.

avatar image CyberMew · Apr 23, 2014 at 01:50 AM 1
Share

How would you unlock it?

avatar image Dimasian · May 17, 2017 at 11:03 AM 0
Share

Thank you! This solution works great to prevent mouse leaving display when using 2 monitors. However it works in fullscreen mode, in windowed RECT boundaries are calculated wrong, don't know why.

avatar image
1

Answer by terdos · Jun 10, 2011 at 04:23 PM

As far as I know you cannot restrict the mouse from going outside the window of the application. However as stated above it is possible to lock the cursor to the center of the screen.

http://unity3d.com/support/documentation/ScriptReference/Screen-lockCursor.html

At this point you will have to draw your own cursor and handle your own cursor events. You can still calculate the position of the cursor using Input.GetAxis("Mouse X") and Input.GetAxis("Mouse Y") assuming they are configured in Project Settings->Input. You will likely have to detect mouse button clicks using Input.GetMouseButtonXXXX().

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 Joshua · Jun 10, 2011 at 04:30 PM 0
Share

Yeah, this works fine and you can of course restrict the position of your own cursor to within the screen. The big drawback though, is that you can no longer use the GUI system. It's why I decided to abandon the goal of restricting the mousepostion in my own question about this http://answers.unity3d.com/questions/59123/lock-mouse-position-with-a-rect-possible.html

avatar image
0

Answer by thenachotech1113 · Jun 19, 2012 at 01:24 AM

what about:

 Screen.lockCursor = true;

i wold put this in function start and ad to the update a button to release it

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 amirabiri · Jun 19, 2012 at 01:44 AM 0
Share

The problem with lockCursor is that it completely cancels the natural operating system mouse movement. For and FPS that's fine. But if you want to use the mouse for something else (like let's say a strategy game) then if you use lockCursor you then have to build your own cursor, which is hard. You have to take care of acceleration and smoothing correctly and account for the user's preferences.

  • 1
  • 2
  • ›

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

20 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

Related Questions

Cursor disappears in unity. 1 Answer

A node in a childnode? 1 Answer

C# Track Mouse Cursor Movement Speed 2 Answers

custom mouse changing when hovering over objects 1 Answer

Raycast based on a Rect? 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