how to stop the movement of the player when a dialogue box appear?

i want to dissable the movement when a dialogue box appear or make the dialogue dissapear when leaving the trigger area help pls

my scrpit of the trashcan

 public class Clickable : MonoBehaviour
 {
     public bool triggered;
     public Dialogue dialogue;
 
     void OnMouseDown()
     {
         if (triggered)
         {
             FindObjectOfType<DialogueManager>().StartDialogue(dialogue);
         }
     }
 
     void OnTriggerEnter()
     {
         if (other.gameObject.CompareTag("Player"))
         {
             triggered = true;
         }
     }
     void OnTriggerExit()
     {
         if (other.gameObject.CompareTag("Player"))
         { 
             triggered = false; 
         }
     }
 }

You got all this way and don’t know how to add a simple if check like this, must of been some good copy pasting :smiley:

Here is some rough pseudo code, but it should point you in the right direction.

public static bool PlayerControlsDisabled = false;

private void OnTriggerEnter(Collider other)
{
    PlayerControlsDisabled = true;
}

private void OnTriggerExit(Collider other)
{
    PlayerControlsDisabled = false;
}

You can then in your PlayerControls check if the bool is true or not

if (yourscriptname.PlayerControlsDisabled == false)
{
    // Player controls in here
}

@ShadyProductions’ answer was close, but had some big issues involved. It immediately disabled player controls on collision instead of when it was clicked on, and it relied on the player walking out of the collider to re-enable their controls, which… wouldn’t be possible.


For starters, (continuing from the previous answer), move the enabled flag to your PlayerControls script, or whatever else you call that script. This is mostly for good-practice reasons to keep it organized.

It should look something like this:

public class PlayerControls : MonoBehaviour
{
    public static bool playerControlsEnabled = true;

    HandleMovement()
    {
        if (playerControlsEnabled)
        {
             // Player controls in here
        }
    }
}

Here’s what your Clickable class should look like:

 public class Clickable : MonoBehaviour
 {
     public bool playerInRange;
     public Dialogue dialogue;
     
     void Activate()
     {
         if (playerInRange)
         {
             FindObjectOfType<DialogueManager>().StartDialogue(dialogue);
             PlayerControls.playerControlsEnabled = false;
         }
     }
 
     private void OnTriggerEnter2D(Collider2D other)
     {
         if (other.gameObject.CompareTag("Player"))
         {
             playerInRange = true;
         }
     }

     private void OnTriggerExit2D(Collider2D other)
     {
         if (other.gameObject.CompareTag("Player"))
         {
             playerInRange = false;
         }
     }
 }

Next you’re going to want your DialogueManager script to re-enable the players controls whenever the dialogue box closes. Just access the bool the same way and set it to true again, and it should all be fine.

That step heavily depends on how that script works, but since we can’t see it, it’s up to you to figure that part out.