Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unity 5.1.1 - Mouse issue

Discussion in 'Editor & General Support' started by Fuzzylr, Jul 23, 2015.

  1. Fuzzylr

    Fuzzylr

    Joined:
    May 8, 2013
    Posts:
    18
    Greetings,

    I created a FPS shooter game for a school assignment. I have one issue I can't really seem to fix. I have this issue for both Mac and Windows. Basically I can't seem to find a way to clamp the mouse to the window. It keeps leaving the window especially when your shooting the gun. This really kind of sucks now that we've added health to the mobs and we are no longer just destroying the prefabs. I've tried the following while it will hide the mouse until it leaves the screen. It doesn't seem to help much with keeping the mouse in the window.

    Cursor.visible = false;
    Cursor.lockState = CursorLockMode.Confined;
     
  2. hoesterey

    hoesterey

    Joined:
    Mar 19, 2010
    Posts:
    659
    you need to set it every frame. (at least that's what I've found) Try putting it in update.
     
  3. Fuzzylr

    Fuzzylr

    Joined:
    May 8, 2013
    Posts:
    18
    Thanks, I will try that now.
     
  4. Fuzzylr

    Fuzzylr

    Joined:
    May 8, 2013
    Posts:
    18
    That worked for me. Only every great once in a while the mouse makes it outside the window. I can live with that. Thank you.
     
  5. hoesterey

    hoesterey

    Joined:
    Mar 19, 2010
    Posts:
    659
    The mouse should never make it outside the window once it has focus unless you hit escape. I do the following in my code as well:
    if (Input.GetMouseButtonDown(1))
    {
    Cursor.lockState = CursorLockMode.Locked;
    }