- Home /
How to disable FPS control but allow mouse to move and interact with GUI
I've been looking around for the answer to this question, but couldn't find one that solved my problem, so forgive me if it has already been answered and I couldn't find it. [First post btw, so don't be harsh on me :P]
I have a scene with Unity's prefab FPS controller. After I detect that the player has clicked on an object (a character), I want to freeze the camera and show a GUI with some dialogue text and options, with which the player should be able to interact.
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("Fire1")) {
RaycastHit hit = new RaycastHit();
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit)) {
if (hit.transform.tag == "NPC")
ShowCharacterDialogue(hit.transform.name);
}
}
}
public void ShowCharacterDialogue (string npcName) {
characterController.enabled = false;
characterController.GetComponent<FirstPersonController>().enabled = false;
characterText.text = "Why are you looking at me?";
foreach(UnityEngine.UI.Button button in buttons) {
button.gameObject.SetActive(true);
}
}
I am able to disable FPS movement and camera rotation and display the GUI successfully after that, but I can't seem to enable mouse interaction. It stays frozen in the center and I can't click on the GUI buttons. Can anyone help me to find out what is it that I'm doing wrong?
Thank you very much in advance!
Sounds like you might need to modify the cursor Lock State
Answer by sandralc · Apr 06, 2016 at 08:30 PM
@ZefanS thank you for your answer! I had actually tried using Lockstate
without success, but I found this thread: http://answers.unity3d.com/questions/1020927/cursorlockstate-not-working.html where it is suggested to set the Lockstate to None
before changing the state again, and it worked!
Cursor.lockState = CursorLockMode.None;
Cursor.lockState = CursorLockMode.Confined;
Cursor.visible = true;
Holy****! You actually solved my problem! It took me forever to figure it out! Thank you!
Your answer
Follow this Question
Related Questions
Unlocking & "Equipping" Colors onto Sprites with c# 2 Answers
Change GUI on mouse over 1 Answer
detect mouseover with grid buttons? 1 Answer
How do i create an object with a GUI Button 1 Answer
Double Click problems with NGUI? 1 Answer