- Home /
How can I make the player stop moving when a UI dialogue box is active?
Ok so I am a beginner at c# and was wondering how to make it so that when a dialogue box appears on the screen, the player can not move at all. I am doing a 2D top-down RPG game, and I have a nice dialogue, and movement script. I would also like to mention that I made a pause menu and figured out how to stop the player from moving when the pause menu is up, but I cant do the same with this dialogue box.
Here's my dialogue script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using TMPro;
public class Dialougue : MonoBehaviour { public TextMeshProUGUI textComponent; public string[] lines; public float textSpeed;
private int index;
// Start is called before the first frame update
void Start()
{
textComponent.text = string.Empty;
StartDialouge();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (textComponent.text == lines[index])
{
NextLine();
}
else
{
StopAllCoroutines();
textComponent.text = lines[index];
}
}
}
void StartDialouge()
{
index = 0;
StartCoroutine(TypeLine());
}
IEnumerator TypeLine()
{
foreach (char c in lines[index].ToCharArray())
{
textComponent.text += c;
yield return new WaitForSeconds(textSpeed);
}
}
void NextLine()
{
if (index < lines.Length - 1)
{
index++;
textComponent.text = string.Empty;
StartCoroutine(TypeLine());
}
else
{
gameObject.SetActive(false);
}
}
}
And here is my player movement script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMovement : MonoBehaviour {
public float moveSpeed = 5f;
public Rigidbody2D rb;
public Animator animator;
Vector2 movement;
public VectorValue startingPosition;
void Start()
{
transform.position = startingPosition.initalValue;
}
void Update()
{
if (!PauseMenu.isPaused)
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
animator.SetFloat("Horizontal", movement.x);
animator.SetFloat("Vertical", movement.y);
animator.SetFloat("Speed", movement.sqrMagnitude);
}
}
void FixedUpdate()
{
rb.MovePosition(rb.position + movement.normalized * moveSpeed * Time.fixedDeltaTime);
}
}
Please, any help will be appreciated. I feel like this has a simple solution that I just don't know.
Answer by cukrhraje · Dec 15, 2021 at 08:15 PM
Basicaly you need to disable movement script. Place this code where you starting the dialogue
gameObject.Find("PLAYER GAME OBJECT NAME OR GAMEOBJECT WITH MOVEMENT SCRIPT ATTACHED TO").GetComponent (PlayerMovement). enabled = false;
And this where you end the dialogue
gameObject.Find("PLAYER GAME OBJECT NAME OR GAMEOBJECT WITH MOVEMENT SCRIPT ATTACHED TO").GetComponent (PlayerMovement). enabled = true;
Hope this will help
When I plug that in I get these two errors:
Severity Code Description Project File Line Suppression State Error CS0176 Member 'GameObject.Find(string)' cannot be accessed with an instance reference; qualify it with a type name instead
Severity Code Description Project File Line Suppression State Error CS0119 'PlayerMovement' is a type, which is not valid in the given context
Answer by Pathojen · Dec 16, 2021 at 02:53 AM
Here's an idea. Add a boolean that is false by default. When the dialogue pops up, have the boolean set to true, and false when the dialogue disappears. Then, only let the player move if the boolean is false.
Your answer
Follow this Question
Related Questions
How do I fix this movement issue? 1 Answer
How to create a lasting UI button that would set a game object to active . 0 Answers
How to Add Knockback Force Based on What Rotation it Came From 2 Answers
I can't get any type of joystick to move my player 1 Answer
BEGINNER: why is my main menu not loading level 1 when I press play? 1 Answer