Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Josh_Stevenson · Dec 01, 2015 at 09:58 AM · c#cameracursor

Cursor hiding issue

Hi all, Im making a simple FPS game and I want to hide the cursor throughout gameplay. It hides the cursor when I click play, but as soon as I click to shoot, it clicks out of the game and the cursor reappears and Im not sure why... I have this code attached to my camera:

 using UnityEngine;
 using System.Collections;
 
 public class CameraController : MonoBehaviour 
 {
     private Camera _camera;
     
     // Use this for initialization
     void Start() 
     { 
         _camera = GetComponent<Camera>();
         
         // Hides the mouse cursor
         Cursor.lockState = CursorLockMode.Locked;
         Cursor.visible = false;
     }
 
     void Update()
     {
 
     }
 }

BTW, Im following along a book called "Unity In Action" and this is the code it had listed. Any solution is much appreciated! :)

Comment
Add comment · Show 6
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 centaurianmudpig · Dec 01, 2015 at 01:32 PM 0
Share

Are you changing the visibility when the mouse is clicked? Probably, but as you do not include the relevant code to the issue we can only guess what is going on.

I can confirm the piece of code you posted is correct for hiding the cursor, which you have confirmed yourself. Show the code that checks for the mouse being pressed and the code it executes which causes the mouse cursor to reappear. Checking that you have not changed the visibility or lockState before you do.

avatar image Josh_Stevenson centaurianmudpig · Dec 01, 2015 at 07:19 PM 0
Share

Here is the code that checks if the mouse is clicked. It's set to fire a bullet when it detects a click.

     using UnityEngine;
         using System.Collections;
             
             public class FireBullet : $$anonymous$$onoBehaviour
             {
                 [SerializeField] private GameObject bulletPrefab;
                 private GameObject _bullet;
             
                 private float delayTime = 0.5f;
                 private float counter = 0f;
             
                 // Use this for initialization
                 void Start () 
                 {
                 }
                 
                 // Update is called once per frame
                 void Update()
                 {
                     if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.$$anonymous$$ouse0) && counter > delayTime)
                     {
                         _bullet = Instantiate(bulletPrefab) as GameObject;
                         _bullet.transform.position = transform.TransformPoint(Vector3.forward);
                         _bullet.transform.rotation = transform.rotation;
                     
                         counter = 0;
                         Destroy(_bullet.gameObject, 1.0f);
                     }
                     counter += Time.deltaTime;
                 }
             }

 
avatar image centaurianmudpig Josh_Stevenson · Dec 01, 2015 at 10:52 PM 0
Share

Nothing obvious there to suggest a problem linked to the mouse showing.

First try out what Dave suggested, does it behave the same in a standalone build? If the problem persists, you need to narrow down the cause. Comment out the code from the FireBullet.Update. Test and see if the cursor appears.

If it doesn't then uncomment a line and retry. For the sake of things I would also remove the $$anonymous$$eyCode.$$anonymous$$ouse0 and try using the space bar for testing purposes. Of course try mouse clicking just to see if it's tied to the cursor beco$$anonymous$$g visible.

Really everything I mention after Dave may seem a load of rubbish and probably is. You say the mouse appears when you shoot a bullet, so that's what my advice is based off. Good hunting.

Show more comments
avatar image Dave-Carlile · Dec 01, 2015 at 08:31 PM 0
Share

How does it behave in a standalone build? The cursor visibility can be kind of wonky when running in the editor apparently.

avatar image Josh_Stevenson Dave-Carlile · Dec 02, 2015 at 01:05 AM 0
Share

It behaves fine in a build. It's just in the editor it has the issue... I guess if I can't find a solution, I'll end up getting used to it. :/ Thank you for your time though, I really appreciate it!

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

sniper zoom in but not out C# 1 Answer

Resetting Camera Position 1 Answer

Take screenshot with no lags Android 0 Answers

Preventing world interactions when using an UI element with an Orthographic 3D camera 0 Answers

Recognize when ever camera looks up and turns back down 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