Search Unity

How to LockCursor Properly for FPS Game

Discussion in 'Web' started by jonkuze, May 16, 2015.

  1. jonkuze

    jonkuze

    Joined:
    Aug 19, 2012
    Posts:
    1,709
    OK so I am trying to use the following LockCursor Script as shown here:
    http://docs.unity3d.com/ScriptReference/Cursor-lockState.html

    but I have had no luck with it actually working properly for a WebGL FPS Game. I need the cursor locked at center of the game window. The script above is good enough to hide the cursor and lock it, but my mouse cursor is actually never centered. I know this because when I try to shoot, the projectiles shoot way off to the left or right where the mouse was apparently locked at.

    How do I click into my WebGL Game and force the Cursor to Lock immediately to the center where the aim would be for an FPS game and ofcoarse hide the Cursor as well.

    Please Advise....
     
  2. Andres-Fernandez

    Andres-Fernandez

    Joined:
    Feb 4, 2013
    Posts:
    57
    If you are aiming/shooting through the center point of the screen, what do you need the cursor for? Just hide it, place some cool sprite at the center of the screen and keep shooting through the center position of the screen...
     
  3. jonkuze

    jonkuze

    Joined:
    Aug 19, 2012
    Posts:
    1,709
    I'm not sure I understand you reply... No I don't need the cursor that's the whole point of hiding the cursor in the first place, but the second part of this is locking the cursor to the center of the game screen where the aim icon is located so you can move the camera around with mouse, aim and shoot. My issue is the hide, lock cursor is not working as intended. It hides the cursor, locks the cursor but it doesn't seem like the scripts provided by Unity are centering the cursor so my shooting aim is totally off. Even if I aim at a target the bullets go flying way off to the left or right of where I am aiming, which indicates that the Cursor is not Locking to the Center of the Screen as it should.
     
  4. jonkuze

    jonkuze

    Joined:
    Aug 19, 2012
    Posts:
    1,709
    Does anyone have a Fix for this? Such a Basic Feature Function I need working for my WebGL Game. Please assist Unity Support!
     
  5. Andres-Fernandez

    Andres-Fernandez

    Joined:
    Feb 4, 2013
    Posts:
    57
    The thing is that you don't need to "center the cursor", it can be left anywhere (as long as it is hidden). Just when you aim/fire, use the center of the screen instead of the cursor/mouse position. There's an exaple in the API here that has a raycast fired right through the middle of the screen. Same with ScreenPointToRay if you use Screen.width/2 and Screen.height/2 to create the ray.
     
    plasmanunchucks, akabedev and jonkuze like this.
  6. jonkuze

    jonkuze

    Joined:
    Aug 19, 2012
    Posts:
    1,709
    Ah!! I see... then I have been doing it all wrong... we are using raycast to fire, but I as you mentioned we have been using the center mouse position, instead we should switch aim/fire (raycast) to center of screen instead. Thanks so much, this may very well solve the problem. I'll keep you posted on the results. Thanks again.