Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Disabling player movement and enabling cursor UNITY 3D

Discussion in 'Scripting' started by Soarino, Nov 20, 2017.

  1. Soarino

    Soarino

    Joined:
    Nov 16, 2017
    Posts:
    23
    I'm trying to implement a dialogue system in which the player approaches an NPC,
    the player presses E and the dialogue commences
    I would like to disable the FPS control camera and movement and enable the mouse so that the player can easily press the continue button to continue through the dialogue without having to press ESC and move with the camera.

    Any help would be greatly appreciated.

    Im using Csharp
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    You could try something like this attached to your FPSController:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityStandardAssets.Characters.FirstPerson;
    4.  
    5. public class EnableMouse : MonoBehaviour {
    6.  
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.  
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update () {
    15.         if (Input.GetKeyDown(KeyCode.E))
    16.         {
    17.             gameObject.GetComponent<FirstPersonController>().enabled = false;
    18.             Cursor.visible = true;
    19.             Cursor.lockState = CursorLockMode.None;
    20.         }
    21.  
    22.     }
    23. }
    24.  
    If you decide to mess around with your controller script, do a backup first and name it something different. Then you can rename it and put it back if it doesn't work out. I would try to disable it first but I haven't done it in a game or anything. I just tried it to see if it would work quickly and it appeared to.
     
    Last edited: Nov 20, 2017
    BeyondMASC and Soarino like this.
  3. Soarino

    Soarino

    Joined:
    Nov 16, 2017
    Posts:
    23
    This works great but how would i get it the script to stop once the dialogue has ended?
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Maybe you want to enable/disable that entire script so hitting 'e' in the middle of nowhere won't disable it (unless you want that).
    If you reference your player on the dialogue scripts, when the dialogue is over you can reset the lock, controller, (and maybe the above script). :)
     
    Soarino likes this.
  5. Soarino

    Soarino

    Joined:
    Nov 16, 2017
    Posts:
    23
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityStandardAssets.Characters.FirstPerson;
    6.  
    7.  
    8. public class DialogueManager : MonoBehaviour {
    9.  
    10.     public Text nameText;
    11.     public Text dialogueText;
    12.  
    13.     public Animator animator;
    14.  
    15.     private Queue<string> sentences;
    16.  
    17.     // Use this for initialization
    18.     void Start() {
    19.         sentences = new Queue<string>();
    20.     }
    21.  
    22.     public void StartDialogue(Dialogue dialogue)
    23.     {
    24.         animator.SetBool("IsOpen", true);
    25.  
    26.         Debug.Log("Starting conversation with " + dialogue.name);
    27.  
    28.         nameText.text = dialogue.name;
    29.  
    30.         sentences.Clear();
    31.  
    32.         foreach (string sentence in dialogue.sentences)
    33.         {
    34.             sentences.Enqueue(sentence);
    35.         }
    36.  
    37.         DisplayNextSentence();
    38.  
    39.     }
    40.  
    41.     public void DisplayNextSentence ()
    42.     {
    43.         if (sentences.Count == 0)
    44.         {
    45.             EndDialogue();
    46.             return;
    47.         }
    48.  
    49.         string sentence = sentences.Dequeue();
    50.         dialogueText.text = sentence;
    51.         Debug.Log(sentence);
    52.     }
    53.  
    54.     void EndDialogue()
    55.     {
    56.         gameObject.GetComponent<FirstPersonController>().enabled = true;
    57.         Cursor.visible = false;
    58.         Cursor.lockState = CursorLockMode.None;
    59.         animator.SetBool("IsOpen", false);
    60.         Debug.Log("End of Conversation.");
    61.     }
    62.  
    63. }


    Ive attached the scripted first mentioned to the FPS controller that all works fine but ive tried adding in at the end of this script at "Void EndDialogue" Code to re-enable the fps controller and disable the cursor etc.

    But when i run the code and hit the end of the dialogue im presented with the Error
    NullReferenceException: Object reference not set to an instance of an object
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, what you want to do is create a variable / reference to the player game object (or one of the scripts on the same game object). Then drag and drop that on your dialogue script.
    From there, you can enable/disable the other script (that does the 'E' key check) and/or you can also use the variable to enable the FirstPersonController.

    Right now, you're trying to access the component (First Person Controller) on the dialogue manager's game object, which .. doesn't have it :)
     
    Soarino likes this.
  7. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Yeah, forget about that other script I gave you and add it all in here:
    just remove that script component with the little circle on the right.
    Code (csharp):
    1.  
    2. public class DialogueManager : MonoBehaviour {
    3. public GameObject fpsController;//drag the fps controller onto here in the editor
    4.  
    5. public void StartDialogue(Dialogue dialogue)
    6. {            fpsController.GetComponent<FirstPersonController>().enabled = false;
    7.            Cursor.visible = true ;        
    8.           Cursor.lockState = CursorLockMode.None;
    9.          //then do the rest of the code that was in here
    10. }
    11.  
    12. //then do the opposite when ending, what you had, but do it to the fpsController object
    13. //you don't have to do anything to the cursor lockstate, because the fpsController will do that.
    14.  
    15.  
    16. }
    17.  
    18.  
    19.  
    20.  
     
    Last edited: Nov 21, 2017
    Soarino likes this.
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    That's good, too ;) heh.
     
  9. Soarino

    Soarino

    Joined:
    Nov 16, 2017
    Posts:
    23
    Works wonders! thanks alot!
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool, glad you got it working :)
     
  11. Skoboo

    Skoboo

    Joined:
    May 9, 2017
    Posts:
    1
    hey, absolute noobie here, I tried to do the same with my simple dialogue that is attached to the fpscontroller's camera
    but there seems to be problems and I don't know what to do?
    upload_2019-7-24_13-1-38.png
    I know this is an old thread but it's the most relevant one to what I'm trying to do :c