Lock mouse to CENTER of screen

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

  1. jemballs

    jemballs

    New Member

    Joined:
    Apr 25, 2009
    Messages:
    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

    New Member

    Joined:
    Apr 25, 2009
    Messages:
    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

    New Member

    Joined:
    Apr 25, 2009
    Messages:
    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

    New Member

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

    Ravenmore

    New Member

    Joined:
    May 12, 2012
    Messages:
    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.
     
  6. HarleysZoonBox

    HarleysZoonBox

    New Member

    Joined:
    Jun 8, 2013
    Messages:
    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

    Member

    Joined:
    Mar 22, 2014
    Messages:
    4
    Nice. Good quick fix!
     
  8. NightmarexGR

    NightmarexGR

    Member

    Joined:
    Jun 7, 2012
    Messages:
    41
    Last edited: May 11, 2014