"Mouse Cursor Lock" Rules on Y8 network:

  • By Default,in gameplay,mouse cursor should never be locked ,rather ask users to initiate the mouse cursor lock event with keyboard keys like key “L” for locking mouse.
    ==> i.e Never user mouse lock state Api in Awake,Start,Update or OnGUI,but only on keyboard key events and cursor should never be locked by default.

    //toggle mouse lock with key “L”.
    if (Input.GetKeyDown (KeyCode.L) ) {
    if(Cursor.lockState == CursorLockMode.None)
    {
    Cursor.visible = false;
    Cursor.lockState = CursorLockMode.Locked;
    }
    else
    {
    Cursor.visible = true;
    Cursor.lockState = CursorLockMode.None;
    }
    }

  • For instance,you can show text in game “To Aim/Shoot well,press “L” to lock your mouse cursor”,this text can stay throughout the gameplay if cursor hasnt locked yet(you can check the lock state of cursor in update method and hence show the required text).

  • Once cursor is locked by user(by pressing key L),you need to instruct user about the way to unlock this locked mouse cursor,this can be done by showing text “Press Escape to Exit Mouse lock” or “Press “L” to toggle mouse lock”.

    //Show required Info to user Clearly".
    void Update {
    if( Cursor.lockState == CursorLockMode.Locked )
    {
    this.GetComponent().text = “Press ‘Escape’ to Exit Mouse Lock.”;
    }
    else
    {
    this.GetComponent().text = “To Aim/Shoot well,Press ‘L’ to Lock Mouse Cursor”;
    }
    }

  • Important: Its mandatory to show Pause Menu Screen, on press of ‘Escape’ and ‘P’ keys of keyboard in gameplay scene,plus mouse should be unlocked as well,which can be coded as shown below.

  • Its Important to note that, you shouldn’t lock mouse cursor back when user press Resume button in pause menu screen,let users rely on keyword L for locking mouse cursor again,and nothing else.

    //Show pause menu screen and unlock mouse cursor with keys Escape and P.
    void Update {
    if((Input.GetKeyDown(KeyCode.P) || Input.GetKeyDown(KeyCode.Escape))
    {
    Cursor.visible = true;
    Cursor.lockState = CursorLockMode.None;
    // enable pause menu screen here
    }
    }
    Here is the live example of game with perfect mouse lock behavior,you could try copying this way.
    [MouseLockPerfectExample] (http://media2.y8.com/y8-studio/unity_webgl_games/u53/block_swat_lock_rules_example/)

  • Note : This Mouse Lock Logic should only be applied during Gameplay and not in menu screens.

2 Likes
Y8 Games