- Home /
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;
}
}
}
sir, Can I see your dialog script I'm making the same thing but the tutorial that I watch is using space to continue not by touch how do you make it touch sir? @Sad_Man30 thank you for your response.
Oh it just a button, I thought you replace the that keycode.space by some touch thing, thank you sir
Answer by oStaiko · Sep 02, 2020 at 05:50 AM
@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.
Nothing wrong with my answer, I never disabled movement in my pseudo code, that is up to the user himself. He only specified to disable player controls and I gave him a template to do it. Not going to write the code for him :)
i specifically say "when a dialogue box appear"
thank you for the answer i finally understand it and your code work ty
sir, Can I see your dialog script I'm making the same thing but the tutorial that I watch is using space to continue not by touch how do you make it touch sir? @Sad_Man30 thank you for your response.
I'm pretty sure a question similar to this has been answered (Try searching how to get mouse input on google) but if you have a question don't comment on an old thread make a new question...
Answer by ShadyProductions · Sep 01, 2020 at 12:16 PM
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 :D
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
}
it's saying to me that PlayerControlsDisabled is assigned but it's value is never used this is what i did
public class Clickable : $$anonymous$$onoBehaviour
{
public bool triggered;
public Dialogue dialogue;
public static bool PlayerControlsDisabled = false;
void On$$anonymous$$ouseDown()
{
if (triggered)
{
FindObjectOfType<Dialogue$$anonymous$$anager>().StartDialogue(dialogue);
}
}
private void OnTriggerEnter2D(Collider2D sad)
{
if (sad.gameObject.CompareTag("Player"))
{
triggered = true;
PlayerControlsDisabled = true;
}
}
private void OnTriggerExit2D(Collider2D sad)
{
if (sad.gameObject.CompareTag("Player"))
{
triggered = false;
PlayerControlsDisabled = false;
}
}
}
and in my player script no error when i added it
https://www.c-sharpcorner.com/UploadFile/e9fdcd/basics-of-C-Sharp/
learn first then apply, not the other way around.
i happen to make the code you give run but it did not even disable anything, it will only disable when i change the code to true
sad