1
votes

I's designing a first person shooting game in unity. I used FPS controller to control the player. Hence, my mouse cursor remains invisible most of the time and when I press Escape, it becomes visible. But, the problem is when I load a new scene from a scene that uses FPS controller, the mouse cursor remains invisible although the new scene does not use FPS controller. Moreover, pressing Escape does not show the mouse cursor in the new scene.

2

2 Answers

1
votes

You can deal with that in a few ways, but here is the core of the problem: Changing the Cursor.visible field is not scene dependent, and it does not get reset when a new scene is loaded. Because of that, you need to set Cursor.visible = true; on the level you load.

I would suggest making a simple script like CursorVisibility that would read this:

public class CursorVisibility : MonoBehaviour
{
    void OnLevelWasLoaded(int level)
    {
        if (FindObjectOfType<FirstPersonController>() != null)
        {
            Cursor.visible = false;
        }
        else
        {
            Cursor.visible = true;
        }
    }
}

Place this on an empty game object in every scene and you have cursor visibility handled automatically.

You can also just place the function:

void OnLevelWasLoaded(int level)
{
    if (FindObjectOfType<FirstPersonController>() != null)
    {
        Cursor.visible = false;
    }
    else
    {
        Cursor.visible = true;
    }
}

in any other script that is unique to the scene without a first person controller.

Just make sure to replace the name of the script with whatever the FPS controller is actually named :)

1
votes

To me the solution was to look for Cursor.visible and Screen.lockCursor parameter and to set:

Cursor.visible = true;
Screen.lockCursor = false;

Or simply remove lockCursor line.