Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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
0
Question by japtar10101 · Jan 14, 2014 at 06:42 AM · mousewebplayermouse cursor

Prevent the mouse from moving out of the webplayer, but still having the cursor movable

I have a game called Galactic Neon, an Asteroids-like twin-stick shooter that uses the cursor as an aiming reticle. One annoying problem I have with the linked webplayer is that I accidentally click outside of the webplayer quite frequently. Is there a way to prevent this from happening? From what I understood, Screen.lockCursor only locks the mouse to the center of the webplayer, which is not what I want.

Comment
Add comment · Show 3
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 thornekey · Jan 14, 2014 at 07:21 AM 0
Share

try this:

 Screen.showCursor = false;


it wont lock the mouse, but just hide in in the game view. if it helps please mark as correct for future reference of others:) Good luck

EDIT: sorry i read it a bit wrong. doing Screen.lockCursor = true;

should only be put in your Start function and then if you wish to change it, change it within the Update function.

avatar image AlucardJay thornekey · Jan 14, 2014 at 08:43 AM 0
Share

this does not answer the question

that uses the cursor as an ai$$anonymous$$g reticle

so the cursor must be able to move freely.

From what I understood, Screen.lockCursor only locks the mouse to the center of the webplayer, which is not what I want.

as the OP states, this is not what they want.

Converting answer to comment.

The OP wants to have the cursor move freely within the game window, but remain confined to the game window.

avatar image ByteSheep · Jan 14, 2014 at 09:17 AM 0
Share

In short: No, there isn't any way that I'm aware of to stop the mouse cursor leaving the bounds of the webplayer.

Browsers do not let you control the mouse position I'm afraid (as this would allow websites to mess with your mouse etc).

You could hide the cursor, but that isn't really going to solve your problem.

One thing you might be able to do is detect clicks which aren't inside of the web player so you could still shoot when outside of the game, but this probably isn't worth the effort..

Anyways, sorry but locking the cursor to only move within the bounds isn't really possible due to browser restrictions :(

Best option is to play the game fullscreen, btw cool game!

2 Replies

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

Answer by japtar10101 · Nov 01, 2014 at 07:04 PM

Thanks to @merry_christmas, I now know it's not possible. Darn.

Edit: I believe starting with Unity 5, it is now possible to confine the mouse to the window of the application.

The behavior is not consistent across platforms, but at least for Windows 10, the follow code works wonders:

 Cursor.lockState = CursorLockMode.Confined;

I am not certain this works with WebGL yet.

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 Bunny83 · Sep 13, 2016 at 09:50 PM

The usual solution in such a case is to use a virtual mouse cursor. So you simply lock and hide the cursor (this also works in WebGL) and simply use your own graphic / image as cursor. This you can move around by reading the mouse deltas with the Input class

 var delta = new Vector3(Input.GetAxis("Mouse X"), -Input.GetAxis("Mouse Y"));
 yourCursorObject.transform.position += delta;

Of course you have to clamp the object position manually so it stays within your desired area. You can also make it wraparound if you like. It's a virtual cursor so you can do what you want. Of course you would need to use the object position as mouse position and not Input.mousePosition. There are many ways how to display such a cursor. The usual way is probably to use Unity's GUI system but a GUITexture would work as well.

Many games use virtual mouse cursors but only for visual reasons. They also hide the hardware cursor and simply display a virtual cursor at the actual cursor position. However using a pure virtual cursor has many advantages. You can set the position of the cursor to any point inside your game, even in WebGL builds. You also can easily freeze the cursor by not applying the delta each frame.

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 Bunny83 · Sep 13, 2016 at 09:57 PM 0
Share

Just have a look at the official $$anonymous$$ozilla pointer lock API. It's the standard that a browser, if it even supports locking the mouse, should offer.

Also have a look at the official pointer lock demo. It's basically exactly what a virtual mouse cursor is. Once the mouse is locked you directly move the circle around just like it is the mouse.

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

23 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

Related Questions

JavaScript: _player.cursor(280) 1 Answer

Following the mouse Cursor does not work correctly after Flip() Function 0 Answers

Custom mouse icon 1 Answer

mouse cursor is not changing in browser 0 Answers

Can't See Mouse Cursor In Game. 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