- Home /
Disable mouselook when entering a trigger
I'm using the FPC. When the game starts(maximize), the mouse cursor is invisible and you can use the mouse to look around. When the player runs into a trigger, I have a canvas become visible and I want the player to have access to the mouse, so they can fill out forms, click on a picture, etc. I've looked all around, watched a few tutorials, but it still not working for some reason. I hope someone can shed some light on this for me. Thanks in advance.
public void OnTriggerEnter()
{
Debug.Log ("Zone Entered!");
ServiceCanvas.enabled = true;
state = true;
Debug.Log (state);
ChangeCursorState ();
}
public void OnTriggerExit()
{
Debug.Log ("Zone Exited!");
ServiceCanvas.enabled = false;
state = false;
Debug.Log (state);
ChangeCursorState ();
}
public void ChangeCursorState()
{
Debug.Log (state);
if(state)
{
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
}
else
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}
}
$$anonymous$$ouse onscreen dont have rigidbody or collider unless you attach it with HUD or gameobject that follow mouse.
Simply your mouse didnt even trigger Enter at all because it have nothing to trigger. Add a collider that follow mouse.
Answer by EXZ_EXZ · Jun 05, 2018 at 11:53 AM
You could try this:
or you could write two seperate functions that get called on either entering or exiting the trigger.
Attach this script to the FPS Controller or the object interacted with?
you need to attach this to the the object that should react to the trigger. if your player has a collider that is marked as a trigger this script needs to be on the object the player is colliding with. if you just want to change the cursor visibility and state you could just use this: private void OnTriggerEnter(Collider other) { if(Cursor.lockState == CursorLock$$anonymous$$ode.None) { Cursor.lockState = CursorLock$$anonymous$$ode.Locked; Cursor.visible = false; } } private void OnTriggerExit(Collider other) { if(Cursor.lockState == CursorLock$$anonymous$$ode.Locked) { Cursor.lockState = CursorLock$$anonymous$$ode.None; Cursor.visible = true; } }
I attached it to the FPS and rewrote it as suggested.
private void OnTriggerEnter(Collider other)
{
ShowCursor ();
}
private void OnTriggerExit(Collider other)
{
HideCursor ();
}
private void ShowCursor ()
{
Debug.Log ("Lockstate enter is:" + Cursor.lockState);
Cursor.lockState = CursorLock$$anonymous$$ode.Locked;
Debug.Log ("Lockstate now is:" + Cursor.lockState);
Cursor.visible = true;
Debug.Log ("Lock it!");
}
private void HideCursor()
{
Debug.Log ("Lockstate is:" + Cursor.lockState);
Cursor.lockState = CursorLock$$anonymous$$ode.None;
Cursor.visible = false;
Debug.Log ("Not Locked!");
}
something is not right, I'm still able to look around the screen with the mouse. I've tried reversing the calls to no avail. I tried just to have the cursor visible which did not work either. Any other thoughts?? Thanks!
attach it to the object you are colliding with and make sure your player has a collider which is marked as "is Trigger" :)
i tried it myself using your code - it worked fine! just make sure your player looks like this and your object has the script attached to it as well as a normal (not trigger) collider.
Let me make sure I understand: Add a collider with trigger checked to the FPS Controller, attach the script to the object interacted with and uncheck the trigger? Is that right?
Also, when the cursor is locked that means you can't look around the room and when it is set to none, you can look around the room?
your player needs a rigidbody for the trigger to work! but beside that everything should work fine! cursorlockstate.none means that your cursor can road around free. if it is set to locked and for example you have an fps controller with the rotation controlled by the mouse - locked means that your rotation will follow your mouse movements. in this case the cursor will always be in the middle of the screen.
like i said i tested it with your code. as long as your player has a collider (trigger) and a rigidbody attached and your object has a collider (collider) and the script attached it should work fine!
$$anonymous$$aybe I'm asking the wrong question. When the trigger goes off and the canvas comes up, I want the cursor to appear(Cursor.visible=true doesn't seem to work running it in the editor, haven't tried a build yet) and be able to act and react as a normal mouse. Then out of the trigger, the cursor goes back to normal mouselook. I do not know why it is not working for me, haven't yet upgraded to the 2018 version, one version behind but it shouldn't make a difference.
Is it possible that the FPController is overriding the script? When I uncheck Lock Cursor in the FPC, the mouse shows up when I run it in the editor.
Answer by Getsumi3 · Jun 06, 2018 at 07:11 PM
Here. You can try like this.
Sorry. I didn't test it so can't tell if it will work
EDT: Okay sorry I missed your question. So as I understand you need to show the canvas and the cursor when player enter the Trigger Zone, right? and then when everything is done you need to close your canvas and hide the cursor. Here's what I suggest:
Create a new script.
public void OnTriggerEnter(Collider other) { //lets check if player entered the trigger zone if(other.gameobject.tag == "Player") { ShowCursor(); //lets check if we pressed ESC to close the canvas if(Input.GetKeyDown(KeyCode.Escape)) { HideCursor(); } } } public void ShowCursor() { ServiceCanvas.enabled = true; Cursor.visible = true; Cursor.lockState = CursorLockMode.None; } public void HideCursor(){ ServiceCanvas.enabled = false; Cursor.visible = false; Cursor.lockState = CursorLockMode.Locked; } }
Create an empty game object
add a box collider and make sure it is marked as 'Is Trigger'
attach the script from above to your trigger zone
Make sure that your player has tag "Player"
Press play and check if everything works fine
I'll check, but the same sort of thing hasn't worked before. I do appreciate your efforts!
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
How to enable gravity on gameobject when interact by player 3 Answers
Left Click Script Problems 1 Answer
Trigger not working 2D 1 Answer