- Home /
Screen.lockCursor Acting Irregular
Hello, I am using this script to control locking of the mouse in Unity WebPlayer, but there are some weird irregularities.
// for Standalone initial screen locking
function Start ()
{
Screen.lockCursor = true; // Start the game with the cursor locked or not
}
function Update () {
if(Screen.lockCursor == false && Input.GetKeyUp("p"))
{
Screen.lockCursor = true;
}
else if (Input.GetKeyUp("p"))
{
Screen.lockCursor = false;
}
}
// Control Screen locking when focusing on the webplayer
function OnApplicationFocus (){
if(Screen.lockCursor == false){
Screen.lockCursor = true;
}
}
When I load the webplayer the mouse is free to move the camera and I must click outside the player and then back to the player to get the mouse lock to activate (using Firefox). However the worst glitch is that I have a bunch of objects in the scene that I am using as triggers (for things like picking up items, turning on lights, starting animations etc) , and when the mouse is not visible from locking the cursor I cannot click any of them. When I start the game if I don't click outside and back in to activate the focus part of the script I can click anything just fine (but the mouse is moving about the scene without being in the middle of course). Every time I toggle the script when locked and centered it I cannot click anywhere though looking around is normal, and as soon as I toggle it off I can click everything again. To make it even more confusing, using the exact same build in the standalone player and in the editor nothing is wrong at all. I would really appreciate help with this as I've spent an entire day trying different configurations to fix this.
Answer by maroonrs2 · May 09, 2012 at 02:39 PM
var i:boolean = true;
function Start()
{
Screen.LockCursor = true; //Correct!
}
function Update()
{
if(i)
{
Screen.LockCursor = true;
}
else if(i != true)
{
Screen.LockCursor = false;
}
if(Input.GetKeyUp(KeyCode.P))
{
if(i)
{
i = false;
}
else if(i != true)
{
i = true;
}
}
}
What I Think It's Doing Is Not Rendering That Screen Is Currently Locked. So Here Is A Simple Declaration, Tested And Everything. Now When You Declare Your Focus, It Only Checks For It Being Locked. In This Case It Hasn't Been Locked Already. So Now You Can Simply Put That Coding In At The End. Any Questions Feel Free To Ask!
Answer by fafase · May 09, 2012 at 03:08 PM
I am not so sure about your description. I would guess what you mean is when playing if you click out of the webplayer you don't control the game anymore. I am probably wrong though. If so, you need to use
function Awake(){
Application.runInBackground = true;
}
if not, I did not get it then. Also you can simplify your code with
function Start ()
{
Screen.lockCursor = true; // Start the game with the cursor locked or not
}
function Update () {
if(Input.GetKeyUp("p"))
{
Screen.lockCursor = !Screen.lockCursor;
}
}
// Control Screen locking when focusing on the webplayer
function OnApplicationFocus (){
if(!Screen.lockCursor){
Screen.lockCursor = true;
}
}
Thank you for simplifying my script!
Sorry for not clarifying, what I meant was that even when I manage to have focus in the webplayer, while Screen.lockCursor is active (and the mouse is hidden and presumably in the middle of the screen) I cannot click any ingame objects(But I can move around and look normally. However when I toggle the locking off ingame (for menus) I can click the objects I should be able to click while the mouse is locked to the center. It seems that I can only click objects (say to turn on a lightswitch with a collider) when the cursor is visible.
I am not familiar with that feature but regarding the documentation http://unity3d.com/support/documentation/ScriptReference/Screen-lockCursor you seem to describe the normal behaviour. I guess if you want to interact with object when it is locked (hidden) you need as you said to add a trigger collider and trigger a gui that would either unlock the mouse pointer or else use the keyboard.
I figured it out! I had a targetting reticle(That had a collider for some reason) that was directly infront of the mouse so when it was in the center it always clicked that ins$$anonymous$$d of the object it was supposed to.. Thank you for all your help!