Forum rules - please read before posting.

Mouse cursor escaping game in windowed mode

edited November 2023 in Technical Q&A

When playing a build in Windowed (not full-screen) mode, when using the mouse, the cursor is able to move out of the game window and interact with other stuff. My game uses the advanced third person camera - camera rotation is done with the mouse and is used constantly throughout the game, so you can see how this would be problematic.
Is there a way to keep the mouse within the game window (unless the user alt-tabs out)?

Lock system cursor when locking AC cursor, Always keep cursor within screen boundary and Confine system cursor to game window are all set to true but do not solve it.
settings

Apparently this issue also occurs if you use dual monitors regardless of using full-screen mode, though I don't currently have a second screen (my last 2nd screen died not long ago) so am unable to confirm this.

My game uses direct-control with a keyboard or controller input method
AC 1.77.4
Unity 2018.4.3

Comments

  • I'm not 100% clear on how the in-game cursor is being manipulated.

    Can you elaborate on how the mouse exactly is used in tandem with Keyboard Or Controller input? Are you dynamically altering the input method when the mouse is used?

  • edited November 2023

    Okay, so for general gameplay in the game, the cursor is hidden & it doesn't really matter which input method is being used, as I have Rewired configured in the game so you can use a controller or k+m - but using a mouse is where the issue occurs because it can escape the window boundary.

    If I change the input method to Mouse and Keyboard, that problem is solved as AC now stops the mouse from escaping the window.

    However, there are several instances in the game in which the (simulated) cursor is made visible and is used for puzzles.
    When using Keyboard Or Controller input the cursor works fine. Yet if I use Mouse And Keyboard input, the cursor becomes visible but will not move from the screen's centre, even though I use a

    Player: Constrain - Cursor Lock: Disable

    action to begin these puzzles.

  • edited November 2023

    btw, here is your script that is used to show / hide the cursor:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using AC;
    
    public class DisplayCursor : MonoBehaviour
    {
        // Start is called before the first frame update
        void Start()
        {
    
        }
        public void ShowCursor()
        {
            AC.KickStarter.cursorManager.cursorDisplay = CursorDisplay.Always;
            AC.KickStarter.settingsManager.hotspotDetection = AC.HotspotDetection.MouseOver;
        }
    
        public void HideCursor()
        {
            AC.KickStarter.cursorManager.cursorDisplay = CursorDisplay.Never;
            AC.KickStarter.settingsManager.hotspotDetection = AC.HotspotDetection.PlayerVicinity;
        }
        // Update is called once per frame
        void Update()
        {
    
        }
    }
    

    This is placed on a GameObject in the scene, and I use an Object: Call Event action to interact with it. When the puzzle begins, showCursor is called which shows the cursor and also changes the hotspot detection from player vicinity to mouse over. HideCursor does the opposite, and is called when the puzzle ends.

  • Is this to say that you're switching dynamically between Mouse And Keyboard and Keyboard Or Controller at runtime?

    AC will only manipulate the locked state of the cursor while using Mouse And Keyboard input. If you're using another input method, you'd need to lock the cursor manually with:

    Cursor.lockState = CursorLockMode.Locked;
    

    When using Keyboard Or Controller input the cursor works fine. Yet if I use Mouse And Keyboard input, the cursor becomes visible but will not move from the screen's centre, even though I use a Player: Constrain - Cursor Lock: Disable action to begin these puzzles.

    This is a separate issue. Is the use of Keyboard Or Controller an effort to get around this, and is it possible that Rewired is affecting the cursor's locked state as well?

  • edited November 2023

    Is this to say that you're switching dynamically between Mouse And Keyboard and Keyboard Or Controller at runtime?

    Oh no, sorry I'm just explaining the results I get from trying out the different input settings.

    Is the use of Keyboard Or Controller an effort to get around this, and is it possible that Rewired is affecting the cursor's locked state as well?

    Keyboard and Controller was just chosen at the very start of the project because I knew the option of using a controller would be integral to this game, it is the setting I have used throughout development without issue really - until I realised the mouse leaving the window problem.

    So, I just discovered, in a build, when using the Mouse and Keyboard input setting (as it fixes the problem of mouse leaving game window), the cursor is able to be locked (and unlocked) again, the only issue is that the cursor will not move from the centre with a controller, but does with a mouse.

    That's odd to me because I assume it's using the same input (CursorHorizontal / CursorVertical), which does work with a controller for e.g rotating the camera. I don't know why a build has different behaviour to in the editor, but it wouldn't be the first time for me.

    Does the cursor rely on a different input when using Mouse and Keyboard maybe?

  • In Mouse And Keyboard, the cursor is moved with raw mouse input.

    In Keyboard Or Controller, it's moved with CursorHorizontal / CursorVertical.

    If you want to move the cursor without the mouse, you'll need to use the latter.

    The cursor can still be moved with the mouse in this mode, as you can map CursorHorizontal to the mouse X axis, etc.

    See my comment above though, about the system cursor's locked state not being affected in this mode. The following will confine it to the boundary, but still allow it to move within:

    Cursor.lockState = CursorLockMode.Confined;
    

    Try placing that in a Start function in a new custom script present in the scene, then relying only on Keyboard Or Controller.

  • Thank you, I have this working now. Only thing is, I have the script on it's own GameObject, so I'd have to go through every single scene in the game and drop this prefab in there. Would it work ok if I just put the script on the PersistentEngine that AC spawns in the scene?

  • Make it a prefab, and then use the Events Editor to create a new Scene: Change: After event. Set the Due to loading save-file to Either, and then have it run an Object: Add or remove Action that spawns in the prefab. This'll occur each time AC opens a scene.

  • That's not an option for me I'm afraid as I'm still on AC 1.77.4.
    No problem though, I'll just have to do it the old fashioned way.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.