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 Willga321 · Feb 26, 2014 at 05:18 PM · mousefreezecursordisable

Mouse won't freeze in center of screen

Hi I am very very new to both unity and javascript. I am using this code to apply a custom cursor to the mouse (a cross hair) and then freeze the mouse to the center of the screen. However it only partially works, the cursor applies just fine and it locks to the screen it just doesn't lock to the center of the screen like I need so it can click on stuff. If anyone knows a good way to do this or a way to fix my code, that would be awesome! Here is what I've come up with so far.

 Screen.lockCursor = true;
 
 var cursorTexture : Texture2D;
 
 var cursorMode : CursorMode = CursorMode.Auto;
 
 var hotSpot : Vector2 = Vector2.zero;
 
     function Update () {
         // In standalone player we have to provide our own key
         // input for unlocking the cursor
         if (Input.GetKeyDown ("escape"))
             Screen.lockCursor = false;
 
     
         Cursor.SetCursor(cursorTexture, hotSpot, cursorMode);
     }



Thanks for any help!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Spoink · Feb 26, 2014 at 05:45 PM

To lock the cursor can be a bit tricky. I've noticed that the Screen.lockCursor wont lock the cursor but move it back to center, leading the mouse to jump around when doing extreme mouse movements.

What I do when I want to make an FPS is to lock the cursor and hide it. Then I do all the gui and hit checks from center of screen. This removes all issues with the mouse.

I only code in C#, but here is what I would do if I were to make a simple shooter.

 private bool isShowingMouse = true;
 
 void Start()
 { ToggleMouse(false); }
 
 void Update()
 { 
    if(Input.GetMouseButtonDown(0))
    { Fire(); }
    if(Input.GetKeyDown(KeyCode.Escape))
    { ToggleMouse(!this.isShowingMouse); }
 }
 
 private void ToggleMouse(bool showMouse)
 {
   Screen.lockCursor = !showMouse;
   Screen.showCursor = showMouse;
 
   this.isShowingMouse = !this.isShowingMouse;
 }
 
 private void Fire()
 {
    Transform cameraTransform = Camera.main.transform;
    Ray fireRay = new Ray(cameraTransform.position, cameraTransform.forward);
 
    RaycastHit hit;
    if(!Physics.Raycast(fireRay, out hit))
    { return; }
 
    Debug.Log("Hit: " + hit.collider.name);
 }

And then you just rotate the camera based on the delta movements from the mouse input. Fyi, I did not test the code, so it might not work directly, but you get the idea :)

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 Willga321 · Feb 26, 2014 at 09:39 PM 0
Share

Thanks so much! I don't know C# but honestly I don't know javascript either, something weird though, the mouse still doesnt center in unity but when I play the stand alone it does. Any idea why that might be?

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

21 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

Related Questions

How can i disable the Mouse Cursor? 3 Answers

Disable/enable script and animation when you move your mouse cursor 1 Answer

Restrict the cursor to a window in Unity 5 0 Answers

JavaScript: _player.cursor(280) 1 Answer

Changing cursor over multiple objects 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