Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Lock mouse to CENTER of screen

Discussion in 'Scripting' started by jemballs, May 4, 2009.

  1. jemballs

    jemballs

    Joined:
    Apr 25, 2009
    Posts:
    8
    Hey guys, as the title suggests I wish to know how to lock the cursor position to the center of the screen.

    Screen.lockCursor doesnt seem to be what I'm after as this hides the cursor (good) and restricts it from leaving the game screen, but i need something that also LOCKS the mouse in the center of the screen.

    Even if there was a way for me to reset the mouse cursor every frame??

    Basically I'm doing a 3rd person shooter with a GUI crosshair, but I want to use onMouseDown on my enemies. So I need my cursor to sit where my crosshair is to make aiming accurate.

    Thanks in advanced.
    Jace
     
  2. jemballs

    jemballs

    Joined:
    Apr 25, 2009
    Posts:
    8
    actually it seems lockCursor DOES lock the mouse.

    However for somereason it isn't locking it in the middle of the screen, its locking the cursor at some arbitrary position above and to the left of the center.

    Any ideas about why it could be doing that???

    Jace
     
  3. jemballs

    jemballs

    Joined:
    Apr 25, 2009
    Posts:
    8
    grr now it IS locking in the center of the screen sometimes and other times it is off at that same random position (Up and to the left of the center of the screen)
     
  4. yellowlabrador

    yellowlabrador

    Joined:
    Oct 20, 2005
    Posts:
    562
  5. Ravenmore

    Ravenmore

    Joined:
    May 12, 2012
    Posts:
    1
    Pardon the necromancy =)

    For prototyping I use this:

    function Update () {
    Screen.lockCursor = true;
    Screen.lockCursor = false;
    }

    Screen.lockCursor moves the pointer to the center of the screen, so it does exactly what the OP suggested - resets the cursor every frame.

    You could also use a MouseMove event for example :)

    Not very clean but it does the job, you retain OnMouseDown functionality, the cursor remains visible.
     
    MillerManeo likes this.
  6. HarleysZoonBox

    HarleysZoonBox

    Joined:
    Jun 8, 2013
    Posts:
    2
    if your using C#
    i use this for setting the value to true or false if paused

    public static bool paused = false;

    void Start()
    {
    Time.timeScale = 1;
    }
    void Update ()
    {

    Screen.lockCursor = true;
    if(paused ==false)
    Screen.lockCursor = false;

    if(Input.GetKeyDown(KeyCode.Escape) paused == false)
    {
    paused = true;
    Time.timeScale = 0;
    }
    else if(Input.GetKeyDown(KeyCode.Escape) paused == true)
    {
    paused = false;
    Time.timeScale = 1;
    }
    }
     
  7. jakejolli

    jakejolli

    Joined:
    Mar 22, 2014
    Posts:
    54
    Nice. Good quick fix!
     
  8. NightmarexGR

    NightmarexGR

    Joined:
    Jun 7, 2012
    Posts:
    217
    Last edited: May 11, 2014