- Home /
Default Mouse Look Script
Hello All,
I have a 3rd person camera on the go with the stardard MouseLook script on my player controlling the X axis and a standard MouseLook script on the camera controlling the Y axis.
What I need is for the cursor to ALWAYS be centered in the middle of the screen. Can this be done? If so can you help me with some example code or point me in the right direction please?
Many thanks in advance!
Answer by runevision · Jan 26, 2010 at 03:11 PM
Use "Screen.lockCursor = true". In standalone games you can do this anywhere, and it should work.
In a webplayer you are not allowed to lock the cursor before the user has clicked somewhere inside the webplayer area, so it's better to set Screen.lockCursor = true whenever you get a mouse-down (if you still want to lock the cursor).
Note that even if the cursor is successfully locked, it will be unlocked again if the user presses ESC. This is why it's a good idea to lock it whenever you get a mousedown. This way the user can free the mouse by pressing ESC, but then get the mouse locked again by pressing inside the webplayer area again.
thank you for your input. I am not going to be having this game in a webplayer, it will be a standalone game. the Screen.lockCursor - true seems to lock the mouse inside the game screen, but I need it locked constantly to the center of the game screen - is there some additional code for this?
Answer by Duke · Jan 26, 2010 at 05:05 PM
Given that locking the mouse cursor in the webplayer is problematic, one possibility you may want to consider is having a crosshair that operates separately from the mouse cursor in your 3rd person mode, such as by drawing one in the OnGUI() event.
thanks for you input. As of yet I have not learnt about this OnGUI() event - if you have any links to tutorials or anything would be much appreciated :-) want to learn as much as possible :-)
I learned about OnGUI() (and a lot of other things) in the 3rd person platformer tutorial. Hopefully it can help you out as well.
http://unity3d.com/support/resources/tutorials/3d-platform-game
Answer by Eric5h5 · Jan 24, 2010 at 10:08 PM
Use "Screen.lockCursor = true".
Thank you for your reply! I have put the Screen.lockCursor = true in the mouseLook script and do not recieve any errors, but it does not lock the cursor. Whereabouts in the script do I need to put this...does it matter where? Sorry very boobish in program$$anonymous$$g.
Thanks in advance
As long as that command is run somewhere, it should work.
again thanks for your reply. Unfortunately it's not doing what I wanted it to. If I move the move right then stop, my character is facing forward, but the mouse cursor is still not central.
$$anonymous$$aybe I was unclear as to what I am trying to achieve. If anyone has ever played the new Call Of Duty game - when you unlock 3rd person games in multiplayer you'll notice the crosshair will ALWAYS stay center of the screen. Hope this makes sense and sorry if I am being dumb! As I said before very new to all this xD
thanks again in advance for all your input