Remapping 'Enable Cursor with Escape'

Gelatinous3

New member
Hoping someone can help me solve this:

I'm attempting to remap the functions of 'Enable Cursor with Escape' from UnityInput to LeftAlt (as well as the keybindings to open any relevant UIS panels requiring mouse input), while simultaneously disabling camera and character movement when the mouse cursor is enabled.

On manipulating the Enable Cursor with Escape function, I am able to successfully remap the cursor display to LeftAlt, however the function to disable the cursor display and return to gameplay is not working. My code is as follows:

Code:
            if (m_EnableCursorWithEscape && UnityEngine.Input.GetKeyDown(KeyCode.LeftAlt)) {
                Cursor.lockState = CursorLockMode.None;
                Cursor.visible = true;
                EventHandler.ExecuteEvent(m_Character, "OnEnableGameplayInput", false);
                // EnableGameplayInput(false);
                if (m_PreventLookVectorChanges) {
                    OnApplicationFocus(false);
                }
            } else if (Cursor.visible && m_DisableCursor && !IsPointerOverUI() && (UnityEngine.Input.GetKeyDown(KeyCode.Mouse0) || UnityEngine.Input.GetKeyDown(KeyCode.Mouse1))) {
                Cursor.lockState = CursorLockMode.Locked;
                Cursor.visible = false;
                EventHandler.ExecuteEvent(m_Character, "OnEnableGameplayInput", true);
                // EnableGameplayInput(false);
                if (m_PreventLookVectorChanges) {
                    OnApplicationFocus(true);
                }

Additionally, the 'OnEnableGameplayInput' functions are not achieving the desired results (nor is 'EnableGameplayInput' which I tried to call directly from the UnityInput script, hence it being commented out atm). I've locked the camera via these lines on the rotate/move functions in the CameraController.cs script which works fine, but does not prevent player movement.

Code:
            if (Cursor.visible == true)
            {
                return;
            }

Currently using Third Person Adventure camera and movement presets, in case that matters - though I couldn't find anything in either of those scrips that would be interfering.
 
Instead of modifying the currently PlayerInput I would instead create a new subclass which adds your own mapping. This will allow you to call OnEnableGameplayInput exactly when you want.
 
Instead of modifying the currently PlayerInput I would instead create a new subclass which adds your own mapping. This will allow you to call OnEnableGameplayInput exactly when you want.

Thanks Justin - subclassing helped get OnEnableGameplayInput to work, however it's still presenting an issue wherein while I can initiate the mouse cursor / gameplay input lock via LeftAlt, attempting to use the same input to re-disable the mouse / return to gameplay still isn't working.
 
What's the code you're using? In the script above, I can only see that the cursor would be unlocked by Alt, but not re-locked.
 
Still using the original UnityInput code as a framework, but it's now it's own subclass and being managed by the component on the character.

Code:
void Update()
    {
        // Enable the cursor if the **ALT** key is pressed. Disable the cursor if it is visbile but should be disabled upon press.
        if (m_EnableCursorWithAlt && !Cursor.visible && UnityEngine.Input.GetKeyDown(KeyCode.LeftAlt))
        {
            Cursor.lockState = CursorLockMode.None;
            Cursor.visible = true;
            EventHandler.ExecuteEvent(m_Character, "OnEnableGameplayInput", false);
        }

        else if (Cursor.visible && m_DisableCursor && !IsPointerOverUI() && UnityEngine.Input.GetKeyDown(KeyCode.Mouse0))
        {
            Cursor.lockState = CursorLockMode.Locked;
            Cursor.visible = false;
            EventHandler.ExecuteEvent(m_Character, "OnEnableGameplayInput", true);
        }
    }

Shouldn't the 'else if' statement be triggering the gameplay to resume / mouse to be disabled again? I've tried removing certain parts of the statement (e.g. only requiring Cursor.visible and an input), as well as embedding it into its own function and calling that function in the else if, but nothing puts the mouse back into disabled mode.
 
I wasn't aware GetKeyDown worked for mouse inputs, I've always used Input.GetMouseButtonDown(0).

In any case, some part of your if/else is not evaluating the way you expect it to, so just go through each one by one (including the first if statement) during runtime (e.g. is m_EnableCursorWithAlt definitely false when you expect it to be false? And so on).
 
Top