- Home /
Stop Unity from hiding mouse cursor?
Hello, I'm making a generic Minecraft clone because I have nothing better to do, but Unity keeps hiding the mouse cursor even when I write code to show it. I'm using Unity's generic First Person Controller as the player.
if (Input.GetKeyDown ("e")){
if (CraftingGridShown) {
CraftingGridShown = false;
CraftingGrid.SetActive (false);
Cursor.visible = false;
}
else{
CraftingGridShown = true;
CraftingGrid.SetActive (true);
Cursor.visible = true;
}
}
EDIT: The issue is that although my code is working, a line of code which is calling some methods is working against it, but I can find a way to access the script so that I can disable it and enable it when needed.
m_MouseLook.LookRotation (transform, m_Camera.transform);
EDIT: I'm trying to directly access the methods of the MouseLook script, but for some reason it keeps complaining about the object reference not set to an instance of an object.
void Start () {
//Cursor.SetCursor (MouseTexture, Vector3.zero, CursorMode.Auto);
TheCam = Camera.main;
MouseLookScript.Init(transform , TheCam.transform);
}
void Update () {
if (Input.GetKeyDown ("e")){
if (CraftingGridShown) {
CraftingGridShown = false;
CraftingGrid.SetActive (false);
Cursor.visible = false;
}
else{
CraftingGridShown = true;
CraftingGrid.SetActive (true);
Cursor.visible = true;
}
}
if (CraftingGridShown == false) {
MouseLookScript.LookRotation (transform, TheCam.transform);
}
Cursor.visible = false;
Change to
//Cursor.visible = false;
Wouldn't that just make it a comment and not do anything?
Answer by $$anonymous$$ · Apr 01, 2018 at 03:48 AM
The default FirstPersonController script was annoying and I only needed a small section of it (the first person view), so I found something easier to work with here I was able to easily switch everything in the script on and off.
using UnityEngine;
using System;
using System.Collections;
public class CameraMouseLook : DavidBehaviour{
Vector2 MouseLook;
Vector2 SmoothV;
public float Sensitivity = 5;
public float Smoothing = 2;
public bool CanMove;
void Update(){
if (CanMove) {
var MD = new Vector2 (Input.GetAxisRaw ("Mouse X"), Input.GetAxisRaw ("Mouse Y"));
MD = Vector2.Scale (MD, new Vector2 (Sensitivity * Smoothing, Sensitivity * Smoothing));
SmoothV.x = Mathf.Lerp (SmoothV.x, MD.x, Smoothing);
SmoothV.y = Mathf.Lerp (SmoothV.y, MD.y, Smoothing);
MouseLook += SmoothV;
Camera.main.transform.localRotation = Quaternion.AngleAxis (-MouseLook.y, Vector3.right);
transform.localRotation = Quaternion.AngleAxis (MouseLook.x, transform.up);
}
}
}
Answer by RVDL_IT · Mar 31, 2018 at 08:10 PM
Have you tried to use Screen.showCursor? I believe that it does practically the same thing.
I'm pretty sure that Screen.showCursor is obselete.
Your answer
![](https://koobas.hobune.stream/wayback/20220612155740im_/https://answers.unity.com/themes/thub/images/avi.jpg)