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! :)
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.
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;
}
}
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.
How does it behave in a standalone build? The cursor visibility can be kind of wonky when running in the editor apparently.
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!